Download presentation
Presentation is loading. Please wait.
1
ArrayList
2
Methods tested on AP add size get set remove
3
Add add(int index, Object o) add(Object o)
Inserts o at index in the list ex: band.add(1, “Ringo”); add(Object o) Inserts o at the end of the list ex: band.add(“John”);
4
Size size() returns the number of elements in the list return type int
Ex: System.out.print(“Band size: “+ band.size());
5
Get get(int index) returns the Object at the given index
since an Object is returned, casting is required in order to use the Object ex: String member = (String)band.get(1);
6
Set set(int index, Object o)
Replaces the element at the specified position (index) in this list with o. ex: band.set(1, “George”); //replaces the object at position 1 with “George” Has a return type of Object ex: System.out.println(band.set(3,"Bob")); //this will print the object that Bob replaced
7
Remove remove(int index)
Removes the element at the specified position (index) in this list. Returns an Object. the Object returned is the element that was removed ex: band.remove(1); ex: band.add(band.remove(1));
8
Other methods: clear() contains(Object elem) indexOf(Object elem)
isEmpty() lastIndexOf(Object elem) removeRange(int fromIndex, int toIndex) toArray()
9
toString public String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object). This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.
10
Inherited Methods Methods inherited from class java.util.
AbstractListequals, hashCode, iterator, listIterator, listIterator, subList AbstractCollectioncontainsAll, remove, removeAll, retainAll, toString Methods inherited from class java.lang. Objectfinalize, getClass, notify, notifyAll, wait, wait, wait Methods inherited from interface java.util. ListcontainsAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeAll, retainAll, subList
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.