Lists and Array List
Jupyter Notebook on using Array List and list manipulations
import java.util.ArrayList;
import java.util.Comparator;
public class ArrayListQuiz {
public static void main(String[] args) {
// Creating 2 arraylists
ArrayList<String> Quiz = new ArrayList<String>();
// ArrayList<String> footballPlayers = new ArrayList<String>();
// Adding without index
Quiz.add("What is the chemical symbol for gold?");
Quiz.add("What is the 5th planet from the sun?");
Quiz.add("What is the acceleration of gravity?");
System.out.println(Quiz);
// Adding players with index
Quiz.add(0, "What is the chemical symbol for helium?");
Quiz.add(1, "What is the 8th planet from the sun?");
System.out.println("setting the questions at certain indexes: " + "\n" + Quiz + "\n");
// Showing size of list
System.out.println("Size of array list: " + "\n" + Quiz.size() + "\n");
// Remove item with index
Quiz.remove(2);
System.out.println("removing the third question at index 2: " + "\n" + Quiz + "\n");
// Remove an item
Quiz.remove("What is the chemical symbol for helium?");
System.out.println("removing the helium question: " + "\n" + Quiz + "\n");
Quiz.add("What is the 8th planet from the sun?");
// get an iteam with index
System.out.println("Get element at index 1: " + "\n" + Quiz.get(1) + "\n");
// find index of an item
System.out.println("indexing the helium question: " + "\n" + Quiz.indexOf("What is the chemical symbol for helium?") + "\n");
// hashcode of list
System.out.println("Hash Code: " + Quiz.hashCode() + "\n");
// check if list is empty
System.out.println("Is the arraylist empty: " + Quiz.isEmpty() + "\n");
// check if list contains an item
System.out.println("Does arraylist contain What is the 8th planet from the sun?" + Quiz.contains("What is the 8th planet from the sun?") + "\n");
// clears list
Quiz.clear();
System.out.println("Cleared arrayList:" + "\n" + Quiz);
}
}
ArrayListQuiz.main(null);