ArrayLists

ArrayLists allows for elements to be added and removed from an array after it is made. In a normal array, a new one has to be made every time

import java.util.ArrayList; // import the ArrayList class

ArrayList<String> mathTools = new ArrayList<String>(); // Create an ArrayList object

add(int index, element)

  • adds an element to the list
mathTools.add("mean");
mathTools.add("median");
mathTools.add("mode");
true

addAll(int index, Collection collection)

  • adds all the elements in a list to another
ArrayList<String> calculus = new ArrayList<String>(); 

calculus.add("derivatives");
calculus.add("integrals");


mathTools.addAll(calculus);
System.out.println("math tools: " + mathTools + "\n");
System.out.println("calc tools: " + calculus);
math tools: [mean, median, mode, derivatives, integrals, derivatives, integrals]

calc: [derivatives, integrals]

size()

  • outputs the size of a list (number of elements)
System.out.println("the number of math tools is " + mathTools.size());
the number of math tools is 7

clear()

  • clears everything in a list
  • reference list is stored
calculus.clear();
System.out.println("calculus: " + calculus);
calculus: []

remove(int index)

  • removes the element from a list with that specific index
  • shifts the rest of the elements to the left and decreases their index by 1
mathTools.remove(0);
System.out.println("math tools: " + mathTools);
math tools: [median, mode, derivatives, integrals, derivatives, integrals]

remove(element)

  • remove the element with a specific name
mathTools.add("mean");

// removes PE Lock
mathTools.remove("mode");
System.out.println("math tools: " + mathTools);
math tools: [median, derivatives, integrals, derivatives, integrals, mean]

get(int index)

  • obtains the element of that index
System.out.println("The first item listed is " + mathTools.get(0));
The first item listed is median

set(int index, element)

  • replaces what is in that index with the new element
// replace the first term (index of 0)
mathTools.set(0, "standard deviation");
System.out.println("math tools: " + mathTools);
math tools: [standard deviation, derivatives, integrals, derivatives, integrals, mean]

IndexOf(element)

  • returns the index of that element
System.out.println("The index for mean is " + mathTools.indexOf("mean"));
The index for mean is 5

lastIndexOf(element)

  • returns last occurrence of an object if not present in list
System.out.println("The true index for mode is " + calculus.lastIndexOf("mode"));
The index for mode is -1

equals(element)

  • true or false if two objects (lists) are equal to one another
if (mathTools.equals(calculus) == true) {
    System.out.println("all the math tools are calculus");
}
else {
    System.out.println("the math tools include noncalculus functions");
}
the math tools include noncalculus functions

hashCode()

  • returns the hashcode of a list
System.out.println("The hashcode for this list is " + mathTools.hashCode());
The hashcode for this list is 2131062898

isEmpty()

  • true or false if the list is empty (has no elements within)
System.out.println("calculus tools empty is " + calculus.isEmpty());
calculus tools empty is true

contains(element)

  • returns true if the list contains the element
System.out.println("math tools contains graphing is " + mathTools.contains("graphing"));
math tools contains graphing is false

containsAll(Collection collection)

  • returns yes if the list contains all the elements of the other list
calculus.add("derivatives");
calculus.add("integrals");

if (mathTools.containsAll(calculus) == true) {
    System.out.println("math tools contains all the calc tools");
}
else {
    System.out.println("math tools does not contain all the calc tools");
}
math tools contains all the calc tools

sort(Comparator comp)

  • sorts elements
Collections.sort(mathTools);
System.out.println("Sorted math tools " + mathTools);
Sorted math tools [derivatives, derivatives, integrals, integrals, mean, standard deviation]