ArrayLists and Lists
Methods for ArrayLists and Lists
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
// Creating 2 arraylists
ArrayList<String> notPro = new ArrayList<String>();
ArrayList<String> Pro = new ArrayList<String>();
// Adding without index
notPro.add("Akhil");
notPro.add("Nakul");
System.out.println("List 1 add() without index:" + "\n" + notPro + "\n");
// list 1 adding with index
notPro.add(0, "Nathan");
notPro.add(2, "Rebecca");
System.out.println("List 1 add() harsha at index 0, and nakul at index 2:" + "\n" + notPro + "\n");
// size()
System.out.println("Size of list 1:" + "\n" + notPro.size() + "\n");
// Adding list 2 to list 1 using addAll()
Pro.add("Federer");
Pro.add("Nadal");
Pro.add("Alcaraz");
notPro.addAll(Pro);
System.out.println("addAll() adding without index:" + "\n" + notPro + "\n");
// Adding list 2 to list 1 with index
notPro.clear();
notPro.add("Akhil");
notPro.add("Nakul");
notPro.add(0, "Nathan");
notPro.add(2, "Rebecca");
notPro.addAll(2, Pro);
System.out.println("addAll() adding at index 2:" + "\n" + notPro + "\n");
// remove(int) index
notPro.remove(6);
System.out.println("remove() at index 6:" + "\n" + notPro + "\n");
// remove(element)
notPro.remove("Rebecca");
System.out.println("remove() Rebecca:" + "\n" + notPro + "\n");
// get(index)
System.out.println("Get element at index 2:" + "\n" + notPro.get(2) + "\n");
// set(index)
System.out.print("Set element at index 2:" + "\n" + "Before: ");
System.out.println(notPro);
notPro.set(2, "Djokovic");
System.out.println("After: " + notPro + "\n");
// indexOf(element)
System.out.println("indexOf Akhil:" + "\n" + notPro.indexOf("Akhil") + "\n");
notPro.add("Akhil"); //temp for next method
// lastIndexOf(element)
System.out.println("Last index of Akhil: " + notPro.lastIndexOf("Akhil") + "\n" + notPro + "\n");
// equals(element)
System.out.println(" Equals Akhil: " + notPro.equals("Akhil") + "\n");
// hascode()
System.out.println("Hash Code: " + notPro.hashCode() + "\n");
// isEmpty()
System.out.println("Is the arraylist empty: " + notPro.isEmpty() + "\n");
// contains(element)
System.out.println("Does arraylist contain 'Akhil': " + notPro.contains("Akhil") + "\n");
// containsAll(collection)
System.out.println("Does arraylist contain Pro collection?: " + "\n" + "Pro collection: " + Pro );
System.out.println(notPro.containsAll(Pro) + "\n");
notPro.add("Federer"); // Adding missing element
System.out.println("Does arraylist contain Pro collection?: " + "\n" + "New Pro collection: " + Pro );
System.out.println(notPro.containsAll(Pro) + "\n");
// sort(comparator)
System.out.println("Unsorted: " + notPro);
notPro.sort(Comparator.naturalOrder());
System.out.println("Sorted using natural alphabetical order comparator: " + notPro + "\n");
// clear()
notPro.clear();
Pro.clear();
System.out.println("Cleared list1:" + "\n" + notPro);
System.out.println("Cleared list2:" + "\n" + Pro);
}
}
Main.main(null);
Trying 2D Arrays to hold Student Information
import java.util.ArrayList;
import java.util.List;
public class Java2DArrayList {
public static void main(String[] args) {
ArrayList<List> arraylist2D = new ArrayList<List>();
List student1=new ArrayList();
student1.add("Akhil");
student1.add("Period 2");
student1.add("Mortensen");
List student2=new ArrayList();
student2.add("Re'em");
student2.add("Period 2");
student2.add("Mortensen");
List student3=new ArrayList();
student3.add("Saathvika");
student3.add("Period 2");
student3.add("Mortensen");
List student4=new ArrayList();
student4.add("Tristan");
student4.add("Period 2");
student4.add("Mortensen");
arraylist2D.add(student1);
arraylist2D.add(student2);
arraylist2D.add(student3);
System.out.println(arraylist2D);
}
}
Java2DArrayList.main(null);