Lists and Array Lists
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. This will probably become your favorite Data Structure in Java.
import java.util.ArrayList;
ArrayList<String> physics = new ArrayList<String>();
add(int index, element)
physics.add("potential energy");
physics.add("kinetic energy");
physics.add("conservation of energy");
addAll(int index, Collection collection)
import java.util.ArrayList;
ArrayList<String> calculators = new ArrayList<String>();
physics.addAll(calculators);
System.out.println("Calculators:" + physics + "\n");
size() This method is used to return the size of the list.
System.out.println("The number of calculators on the website is currently " + physics.size());
clear() This method is used to remove all the elements in the list. However, the reference of the list created is still stored.
calculators.clear();
System.out.println("Calculators: " + calculators);
remove(int index) This method removes an element from the specified index. It shifts subsequent elements(if any) to left and decreases their indexes by 1.
physics.remove(0);
System.out.println("Calculators: " + physics);
remove(element) This method is used to remove the first occurrence of the given element in the list.
physics.add("Work");
physics.remove("Potential Energy");
System.out.println("Calculators" + physics);
get(int index) This method returns elements at the specified index.
System.out.println("The first calculator is " + physics.get(0));
set(int index, element) This method replaces elements at a given index with the new element. This function returns the element which was just replaced by a new element.
physics.set(0, "Kinetic Energy");
System.out.println("Calculators: " + physics);
indexOf(element) This method returns the first occurrence of the given element or -1 if the element is not present in the list.
System.out.println("The gravity calculator is " + physics.indexOf("the first calculator"));
lastIndexOf(element) This method returns the last occurrence of the given element or -1 if the element is not present in the list.
System.out.println("The potential energy calculator is " + physics.indexOf("Physics Tutoring 1 hr sessions"));
equals(element) This method is used to compare the equality of the given element with the elements of the list.
if (physics.equals(calculators) == true) {
System.out.println("The inventory matches the listings!");
}
else {
System.out.println("The inventory does not match the listings!");
}
hashCode() This method is used to return the hashcode value of the given list.
System.out.println("The hashcode for this list is " + physics.hashCode());
isEmpty() This method is used to check if the list is empty or not. It returns true if the list is empty, else false.
System.out.println("Calculators is empty " + physics.isEmpty());
contains(element) This method is used to check if the list contains the given element or not. It returns true if the list contains the element.
System.out.println("Calculators contains potential energy is " + physics.contains("potential energy"));
containsAll(Collection collection) This method is used to check if the list contains all the collection of elements.
if (physics.containsAll(calculators) == true) {
System.out.println("The calculators are all working");
}
else {
System.out.println("The calculators are broken");
}
sort(Comparator comp) This method is used to sort the elements of the list on the basis of the given comparator.
Collections.sort(physics);
System.out.println("Calculators arranged " + physics);