Java Reference
In-Depth Information
Display 14.1
Some Methods in the Class ArrayList (part 2 of 3)
public Base_Type get( int index)
Returns the element at the specified index. This statement is analogous to returning a[index]
for an array a . The index must be a value greater than or equal to 0 and less than the current size
of the ArrayList . Throws IndexOutOfBoundsException if the index is not in this range.
METHODS TO ADD ELEMENTS
public boolean add( Base_Type newElement)
Adds the specified element to the end of the calling ArrayList and increases the ArrayList 's
size by one. The capacity of the ArrayList is increased if that is required. Returns true if the add is
successful. (The return type is boolean , but the method is typically used as if it were a void method.)
public void add( int index, Base_Type newElement)
Inserts newElement as an element in the calling ArrayList at the specified index. Each element in
the ArrayList with an index greater or equal to index is shifted upward to have an index that is one
greater than the value it had previously. The index must be a value greater than or equal to 0 and less
than or equal to the current size of the ArrayList . Throws IndexOutOfBoundsException if the
index is not in this range. Note that you can use this method to add an element after the last element.
The capacity of the ArrayList is increased if that is required.
METHODS TO REMOVE ELEMENTS
public Base_Type remove( int index)
Deletes and returns the element at the specified index. Each element in the ArrayList with
an index greater than index is decreased to have an index that is one less than the value it had
previously. The index must be a value greater than or equal to 0 and less than the current size
of the ArrayList . Throws IndexOutOfBoundsException if the index is not in this range.
Often used as if it were a void method.
protected void removeRange( int fromIndex, int toIndex)
Deletes all the elements with indices i such that fromIndex ≤ i < toIndex . Elements with
indices greater than or equal to toIndex are decreased appropriately.
public boolean remove(Object theElement)
Removes one occurrence of theElement from the calling ArrayList . If theElement is
found in the ArrayList , then each element in the ArrayList with an index greater than
the removed element's index is decreased to have an index that is one less than the value it
had previously. Returns true if theElement was found (and removed). Returns false if
theElement was not found in the calling ArrayList .
public void clear()
Removes all elements from the calling ArrayList and sets the ArrayList 's size to zero.
SEARCH METHODS
public boolean contains(Object target)
Returns true if the calling ArrayList contains target ; otherwise, returns false . Uses the method
equals of the object target to test for equality with any element in the calling ArrayList .
 
Search WWH ::




Custom Search