org.clapper.util.misc
Class SparseArrayList<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<T>
              extended by org.clapper.util.misc.SparseArrayList<T>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>, java.util.RandomAccess

public class SparseArrayList<T>
extends java.util.ArrayList<T>

SparseList implements a sparse array. This class is identical to java.util.ArrayList, except that it permits assignment to array indexes that are beyond the current length of the list, using the expected set() and add() methods. The list is extended and null-filled in that case.

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be wrapped using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

        List list = Collections.synchronizedList(new SparseList());
 

The iterators returned by this class's iterator and listIterator methods are fail-fast: if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Version:
$Revision: 6941 $
See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
SparseArrayList()
          Allocate a new SparseList object with the same default initial capacity as a java.util.ArrayList object created with its default constructor.
SparseArrayList(java.util.Collection<T> c)
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
SparseArrayList(int initialCapacity)
          Constructs an empty list with the specified initial capacity.
 
Method Summary
 void add(int index, T element)
          Inserts the specified element at the specified position in this list.
 T set(int index, T element)
          Replaces the element at the specified position in this list with the specified element.
 
Methods inherited from class java.util.ArrayList
add, addAll, addAll, clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeRange, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
 

Constructor Detail

SparseArrayList

public SparseArrayList()
Allocate a new SparseList object with the same default initial capacity as a java.util.ArrayList object created with its default constructor.


SparseArrayList

public SparseArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.

Parameters:
initialCapacity - the initial capacity of the list.
Throws:
java.lang.IllegalArgumentException - if the specified initial capacity is negative

SparseArrayList

public SparseArrayList(java.util.Collection<T> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The SparseList instance has an initial capacity of 110% the size of the specified collection.

Parameters:
c - the collection whose elements are to be placed into this list.
Throws:
java.lang.NullPointerException - if the specified collection is null.
Method Detail

add

public void add(int index,
                T element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). If the specified index is beyond the end of the list, then this method behaves exactly like set(int,Object).

Specified by:
add in interface java.util.List<T>
Overrides:
add in class java.util.ArrayList<T>
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
java.lang.IndexOutOfBoundsException - if the index is negative.

set

public T set(int index,
             T element)
      throws java.lang.IndexOutOfBoundsException
Replaces the element at the specified position in this list with the specified element. Unlike a regular ArrayList, this method does not throw an exception if the passed-in index is beyond the end of the array; instead, it extends the array (via ensureCapacity()) so that the index is legal. (But, the index must be positive.)

Specified by:
set in interface java.util.List<T>
Overrides:
set in class java.util.ArrayList<T>
Parameters:
index - index of element to replace or store
element - element to be stored at the specified position
Returns:
the element previously at the specified position, or null
Throws:
java.lang.IndexOutOfBoundsException - if index is negative


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.