ArrayList PPT
Download
Report
Transcript ArrayList PPT
ArrayList
Methods tested on AP
add
size
get
set
remove
Add
add(int index, 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”);
Size
size()
returns the number of elements in the list
return type int
Ex:
System.out.print(“Band size: “+ band.size());
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);
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
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));
Other methods:
clear()
contains(Object elem)
indexOf(Object elem)
isEmpty()
lastIndexOf(Object elem)
removeRange(int fromIndex, int toIndex)
toArray()
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.
Inherited Methods
Methods inherited from class java.util.
Methods inherited from class java.util.
AbstractCollectioncontainsAll, remove, removeAll,
retainAll, toString
Methods inherited from class java.lang.
AbstractListequals, hashCode, iterator, listIterator,
listIterator, subList
Objectfinalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.
ListcontainsAll, equals, hashCode, iterator, listIterator,
listIterator, remove, removeAll, retainAll, subList