ArrayLists
using array lists to hold data
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");
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);
size()
- outputs the size of a list (number of elements)
System.out.println("the number of math tools is " + mathTools.size());
clear()
- clears everything in a list
- reference list is stored
calculus.clear();
System.out.println("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);
remove(element)
- remove the element with a specific name
mathTools.add("mean");
// removes PE Lock
mathTools.remove("mode");
System.out.println("math tools: " + mathTools);
get(int index)
- obtains the element of that index
System.out.println("The first item listed is " + mathTools.get(0));
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);
IndexOf(element)
- returns the index of that element
System.out.println("The index for mean is " + mathTools.indexOf("mean"));
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"));
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");
}
hashCode()
- returns the hashcode of a list
System.out.println("The hashcode for this list is " + mathTools.hashCode());
isEmpty()
- true or false if the list is empty (has no elements within)
System.out.println("calculus tools empty is " + calculus.isEmpty());
contains(element)
- returns true if the list contains the element
System.out.println("math tools contains graphing is " + mathTools.contains("graphing"));
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");
}
sort(Comparator comp)
- sorts elements
Collections.sort(mathTools);
System.out.println("Sorted math tools " + mathTools);