Rohan G Array List
Week 7 Array List
import java.util.ArrayList;
import java.util.Comparator;
public class ArrayList1 {
public static void main(String[] args) {
// Creating an arraylist Jeopardy
ArrayList<String> Jeopardy = new ArrayList<String>();
ArrayList<Integer> JeopardyPts = new ArrayList<Integer>();
// Adding without index
Jeopardy.add("What is the capital of Chile?");
Jeopardy.add("What is the capital of France?");
Jeopardy.add("What is the capital of Brazil?");
JeopardyPts.add(1);
JeopardyPts.add(2);
JeopardyPts.add(7);
JeopardyPts.add(9);
JeopardyPts.add(5);
System.out.println(Jeopardy);
System.out.println(JeopardyPts);
// Adding players with index
Jeopardy.add(0, "What is the capital of England?");
Jeopardy.add(1, "What is the capital of Ethiopia");
System.out.println("setting the questions at certain indexes: " + "\n" + Jeopardy + "\n");
// Showing size of list
System.out.println("Size of array list: " + "\n" + Jeopardy.size() + "\n");
// Remove item with index
Jeopardy.remove(3);
System.out.println("removing the third question at index 3: " + "\n" + Jeopardy + "\n");
// Remove an item
Jeopardy.remove("What is the capital of England?");
System.out.println("removing the england question: " + "\n" + Jeopardy + "\n");
Jeopardy.add("What is the capital of England?");
// get an iteam with index
System.out.println("Get element at index 2: " + "\n" + Jeopardy.get(2) + "\n");
// find index of an item
System.out.println("indexing the england question: " + "\n" + Jeopardy.indexOf("What is the capital of England?") + "\n");
// hashcode of list
System.out.println("Hash Code: " + Jeopardy.hashCode() + "\n");
// check if list is empty
System.out.println("Is the arraylist empty: " + Jeopardy.isEmpty() + "\n");
// check if list contains an item
System.out.println("Does arraylist contain What is the capital of England?: " + Jeopardy.contains("What is the capital of England?") + "\n");
// clears list
Jeopardy.clear();
System.out.println("Cleared arrayList:" + "\n" + Jeopardy);
}
}
ArrayList1.main(null);