de.enough.polish.util
Class ArrayList<K>

java.lang.Object
  extended by de.enough.polish.util.ArrayList<K>
Type Parameters:
K - when you use the enough-polish-client-java5.jar you can parameterize the ArrayList, e.g. ArrayList<Integer> = new ArrayList<Integer>(10);

Copyright (c) Enough Software 2005 - 2009

All Implemented Interfaces:
Externalizable, Serializable

public class ArrayList<K>
extends Object
implements Externalizable

Provides an flexible list for storing objects.

This ArrayList is mostly compatible with the java.util.ArrayList of the J2SE. It lacks, however, some not often used methods. Also some methods like add or remove do not return a boolean value, since true is always returned (or an exception is thrown) anyhow. This way we can save some precious space!

Workarounds for some of the missing methods:

Author:
Robert Virkus, robert@enough.de

Constructor Summary
ArrayList()
          Creates an ArrayList with the initial capacity of 10 and a growth factor of 75%
ArrayList(int initialCapacity)
          Creates an ArrayList with the given initial capacity and a growth factor of 75%
ArrayList(int initialCapacity, int growthFactor)
          Creates a new ArrayList
 
Method Summary
 void add(int index, K element)
          Inserts the given element at the defined position.
 void add(K element)
          Stores the given element in this list.
 void addAll(ArrayList list)
          Adds all elements of the given list to this list.
 void addAll(Object[] elements)
          Adds all elements to this list.
 void addAll(Object[] elements, int offset, int len)
          Adds all elements to this list.
 void clear()
          Removes all of the elements from this list.
 boolean contains(K element)
          Determines whether the given element is stored in this list.
 K get(int index)
          Returns the element at the specified position in this list.
 Object[] getInternalArray()
          Retrieves the internal array - use with care! This method allows to access stored objects without creating an intermediate array.
 int indexOf(K element)
          Retrieves the index of the given object.
 void read(DataInputStream in)
          Restores the internal instance fields from the given input stream.
 K remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(K element)
          Removes the given element.
 K set(int index, K element)
          Replaces the element at the specified position in this list with the specified element.
 int size()
          Retrieves the current size of this array list.
 Object[] toArray()
          Returns all stored elements as an array.
 K[] toArray(K[] target)
          Returns all stored elements in the given array.
 String toString()
          Returns String containing the String representations of all objects of this list.
 void trimToSize()
          Trims the capacity of this ArrayList instance to be the list's current size.
 void write(DataOutputStream out)
          Stores the internal instance fields to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ArrayList

public ArrayList()
Creates an ArrayList with the initial capacity of 10 and a growth factor of 75%


ArrayList

public ArrayList(int initialCapacity)
Creates an ArrayList with the given initial capacity and a growth factor of 75%

Parameters:
initialCapacity - the capacity of this array list.

ArrayList

public ArrayList(int initialCapacity,
                 int growthFactor)
Creates a new ArrayList

Parameters:
initialCapacity - the capacity of this array list.
growthFactor - the factor in % for increasing the capacity when there's not enough room in this list anymore
Method Detail

size

public int size()
Retrieves the current size of this array list.

Returns:
the number of stored elements in this list.

contains

public boolean contains(K element)
Determines whether the given element is stored in this list.

Parameters:
element - the element which might be stored in this list
Returns:
true when the given element is stored in this list
Throws:
IllegalArgumentException - when the given element is null
See Also:
remove(Object)

indexOf

public int indexOf(K element)
Retrieves the index of the given object.

Parameters:
element - the object which is part of this list.
Returns:
the index of the object or -1 when the object is not part of this list.
Throws:
IllegalArgumentException - when the given element is null

get

public K get(int index)
Returns the element at the specified position in this list.

Parameters:
index - the position of the desired element.
Returns:
the element stored at the given position
Throws:
IndexOutOfBoundsException - when the index < 0 || index >= size()

remove

public K remove(int index)
Removes the element at the specified position in this list.

Parameters:
index - the position of the desired element.
Returns:
the element stored at the given position
Throws:
IndexOutOfBoundsException - when the index < 0 || index >= size()

remove

public boolean remove(K element)
Removes the given element.

Parameters:
element - the element which should be removed.
Returns:
true when the element was found in this list.
Throws:
IllegalArgumentException - when the given element is null
See Also:
contains(Object)

clear

public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.


add

public void add(K element)
Stores the given element in this list.

Parameters:
element - the element which should be appended to this list.
Throws:
IllegalArgumentException - when the given element is null
See Also:
add( int, Object )

add

public void add(int index,
                K element)
Inserts the given element at the defined position. Any following elements are shifted one position to the back.

Parameters:
index - the position at which the element should be inserted, use 0 when the element should be inserted in the front of this list.
element - the element which should be inserted
Throws:
IllegalArgumentException - when the given element is null
IndexOutOfBoundsException - when the index < 0 || index >= size()

set

public K set(int index,
             K element)
Replaces the element at the specified position in this list with the specified element.

Parameters:
index - the position of the element, the first element has the index 0.
element - the element which should be set
Returns:
the replaced element
Throws:
IndexOutOfBoundsException - when the index < 0 || index >= size()

toString

public String toString()
Returns String containing the String representations of all objects of this list.

Overrides:
toString in class Object
Returns:
the stored elements in a String representation.

toArray

public Object[] toArray()
Returns all stored elements as an array.

Returns:
the stored elements as an array.

toArray

public K[] toArray(K[] target)
Returns all stored elements in the given array.

Parameters:
target - the array in which the stored elements should be copied.
Returns:
the stored elements of this list, when the target array is smaller than the size of this ArrayList, only the elements that fit in the target array are returned

trimToSize

public void trimToSize()
Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.


getInternalArray

public Object[] getInternalArray()
Retrieves the internal array - use with care! This method allows to access stored objects without creating an intermediate array. You really should refrain from changing any elements in the returned array unless you are 110% sure about what you are doing. It is safe to cycle through this array to access it's elements, though. Note that some array positions might contain null. Also note that the internal array is changed whenever this list has to be increased.

Returns:
the internal array

addAll

public void addAll(ArrayList list)
Adds all elements of the given list to this list.

Parameters:
list - the list
Throws:
NullPointerException - when the specified list is null

addAll

public void addAll(Object[] elements)
Adds all elements to this list.

Parameters:
elements - the array elements
Throws:
NullPointerException - when an element is null or the array is null

addAll

public void addAll(Object[] elements,
                   int offset,
                   int len)
Adds all elements to this list.

Parameters:
elements - the array elements
offset - the start index
len - the number of elements that should be copied
Throws:
NullPointerException - when an element is null or the array is null

read

public void read(DataInputStream in)
          throws IOException
Description copied from interface: Externalizable
Restores the internal instance fields from the given input stream.

Specified by:
read in interface Externalizable
Parameters:
in - the input stream from which the data is loaded
Throws:
IOException - when reading fails

write

public void write(DataOutputStream out)
           throws IOException
Description copied from interface: Externalizable
Stores the internal instance fields to the output stream.

Specified by:
write in interface Externalizable
Parameters:
out - the output stream to which instance fields should be written
Throws:
IOException - when writing fails