ArrayLists
Show usage of Lists interfaces in a Jupyter Notebook example. Try using List interface and making a ArrayList object. Consider using an example that will be part of interest or final projects
import java.util.ArrayList;
import java.util.Comparator;
// Create an ArrayList object
ArrayList<String> artists = new ArrayList<String>();
// add(int index, element)
artists.add("Taylor Swift");
artists.add("50 Cent");
artists.add("Miley Cyrus");
System.out.println("- add(int index, element)");
System.out.println(artists + "\n");
// addAll(int index, Collection collection)
ArrayList<String> songs = new ArrayList<String>();
songs.add("Shake It Off");
songs.add("In Da Club");
songs.add("Party In The USA");
artists.addAll(songs);
System.out.println("- addAll(int index, Collection collection)");
System.out.println(artists + "\n");
// size()
System.out.println("- size()");
System.out.println("There are " + artists.size() + " terms in the ArrayList artists");
System.out.println("There are " + songs.size() + " terms in the ArrayList songs" + "\n");
// clear()
songs.clear();
System.out.println("- clear()");
System.out.println(songs+ "\n");
// remove(int index)
artists.remove(5);
System.out.println("- remove(int index)");
System.out.println(artists + "\n");
// remove(element)
artists.remove("Miley Cyrus");
System.out.println("- remove(element)");
System.out.println(artists + "\n");
// get(int index)
System.out.println("- get(int index)");
System.out.println(artists.get(1) + "\n");
// set(int index, element)
artists.set(3, "Candy Shop");
System.out.println("- set(int index, element)");
System.out.println(artists + "\n");
// indexOf(element)
System.out.println("- indexOf(element)");
System.out.println(artists.indexOf("50 Cent") + "\n");
// lastIndexOf(element)
System.out.println("- lastIndexOf(element)");
System.out.println(artists.indexOf("Candy Shop") + "\n");
// equals(element)
System.out.println("- equals(element)");
System.out.println(artists.equals("Snoop Dogg"));
// hashCode()
System.out.println("\n" + "- hashCode()");
System.out.println(artists.hashCode() + "\n");
// isEmpty()
System.out.println("- isEmpyty()");
System.out.println(artists.isEmpty() + "\n");
// contains(element)
System.out.println("- contains(element)");
System.out.println(artists.contains("50") + "\n");
// containsAll(Collection collection)
System.out.println("- containsAll(Collection collection)");
System.out.println(artists.contains(songs) + "\n");
// sort(Comparator comp)
System.out.println("- sort(Comparator comp)");
artists.sort(Comparator.naturalOrder());
System.out.println(artists);