Array List Notebook
public class Main {
public static void main(String args[]) {
new ArrayList();
ArrayList<String> Students = new ArrayList<String>(); // main arraylist
Students.add("Samuel");
Students.add("Kian");
System.out.println("Students List: " + Students);
ArrayList<String> Students2 = new ArrayList<String>(); //second arraylist to add to main arraylist
Students2.add("Calissa");
Students2.add("Evan");
Students2.add("Mr Mortensen");
Students.addAll(Students2); //adds elements from second array to main array
System.out.println("Students List with students added from second array: " + Students);
System.out.println("Number of Students: " + Students.size());
Students2.clear(); //clear an ArrayList
System.out.println("Second array emptied: " + Students2);
System.out.println("The second array is empty: " + Students2.isEmpty());
if (Students.contains("Mr Mortensen") == true) {
Students.remove("Mr Mortensen");
Collections.sort(Students);
System.out.println("Mr. Mortensen removed and students sorted alphabetically: " + Students);
}
else {
Collections.sort(Students);
System.out.println("Students sorted alphabetically: " + Students);
}
System.out.println("2nd student from sorted list: " + Students.get(2));
var b = Students.indexOf("Samuel");
b++;
System.out.println("Samuel is the " + b + "th person in the list");
System.out.println("Amount of students: " + Students.size());
System.out.println("Is bob part of the list: " + Students.contains("bob"));
System.out.println("Hashcode of list: " + Students.hashCode());
}
}
Main.main(null)