array list Flashcards
generics
effectively build a template than can be instantiated in many ways (form of polymorphism)
declare an array list
ArrayList arrName;
create a new ArrayList object and store the reference.
arrName = new ArrayList();
how to add objects to an array list
arrName.add(1.1F); or Float f = new Float(3.14F); arrName.add(f); or specify position arrName.add(2.3F);
how to remove items from array list
arrName.remove(three);
use reference to the object in list (returns Boolean (if that item was in the list or not))
or
arrName.remove(1);
specify its 0-based index position number in list ( returns a reference to removed object)
contains method
(arrName.contains(seek));
returns a Boolean value indicating whether that object is in the list.
get method
returns a reference to the object at the 0-based index position
Student ref = myStudents.get(3); ref.setUID(987654321);
to find the current length of an array
int length = arrName.size();
iterate through array list
int length = arrName.size(); for (int i=0; I
shallow copy array list code
ArrayList newArr; newArr = new ArrayList(oldArr);
for each loop
also allows us to iterate through array lists
for (Typename iteratedVal : collection) { //process the iteratedVal object
}
NOTE: You cannot alter the structure of a list while iterating through it using a for-each style loop!!!
deep copy array list code
ArrayList dupList; dupList = new ArrayList(); for (StringBuffer str : origList) { dupList.add(new StringBuffer(str)); }