Arrays | ArrayLists |
---|---|
random-access, linear data structure | random-access, linear data structure |
fixed size once created | dynamic size; grows automatically |
can contain objects and primitives | can only contain objects |
must declare element type | element type is Object |
safe: run-time bounds checking | safe: run-time bounds checking |
Fish[] myArray = new Fish[15]; | ArrayList myList = new ArrayList(); |
myArray[index] = new Fish(loc); | myList.add(new Fish(loc)); |
myList.set(index, new Fish(loc)); | |
Fish f = myArray[index]; | Fish f = (Fish) myList.get(index); |
myArray[index].move(); | ((Fish) myList.get(index)).move(); |
for ( int k = 0; k < myArray.length; k++ ) System.out.println(myArray[index]); |
for ( int k = 0; k < myList.size(); k++ ) System.out.println(myList.get(k)); |
Methods in AP Subset: | |
boolean add(Object obj) | |
void add(int index, Object obj) | |
Object get(int index) | |
Object set(int index, Object obj) | |
Object remove(int index) | |
boolean remove(Object obj) - MBS only | |
int size() | |
Iterator iterator() | |
Iterator listIterator() |