Arraylists
Arraylist functions
import java.util.ArrayList;
import java.util.Comparator;
public class ArrayListProgram {
public static void main(String[] args) {
// Creating 2 arraylists
ArrayList<String> basketballPlayers = new ArrayList<String>();
// ArrayList<String> footballPlayers = new ArrayList<String>();
// Adding without index
basketballPlayers.add("LeBron James");
basketballPlayers.add("Stephen Curry");
System.out.println("Basketball Players are" + "\n" + basketballPlayers + "\n");
// Adding players with index
basketballPlayers.add(0, "Kobe Bryant");
basketballPlayers.add(2, "Carmelo Anthony");
System.out.println("Kobe at index 0, and Carmelo at index 2:" + "\n" + basketballPlayers + "\n");
// Showing size of list
System.out.println("Size of basketball players list:" + "\n" + basketballPlayers.size() + "\n");
// Remove item with index
basketballPlayers.remove(3);
System.out.println("remove() at index 3:" + "\n" + basketballPlayers + "\n");
// Remove an item
basketballPlayers.remove("Kobe Bryant");
System.out.println("remove() Kobe:" + "\n" + basketballPlayers + "\n");
basketballPlayers.add("Kobe Bryant");
// get an iteam with index
System.out.println("Get element at index 2:" + "\n" + basketballPlayers.get(2) + "\n");
// find index of an item
System.out.println("indexOf Kobe:" + "\n" + basketballPlayers.indexOf("Kobe Bryant") + "\n");
// hashcode of list
System.out.println("Hash Code: " + basketballPlayers.hashCode() + "\n");
// check if list is empty
System.out.println("Is the arraylist empty: " + basketballPlayers.isEmpty() + "\n");
// check if list contains an item
System.out.println("Does arraylist contain Kobe Bryant: " + basketballPlayers.contains("Kobe Bryant") + "\n");
// clears list
basketballPlayers.clear();
System.out.println("Cleared basketball Players:" + "\n" + basketballPlayers);
}
}
ArrayListProgram.main(null);