FileDocCategorySizeDatePackage
IntList.javaAPI DocExample2123Sat Jun 02 02:42:10 BST 2001None

IntList

public class IntList extends Object implements Serializable
A simple class that implements a growable array or ints, and knows how to serialize itself as efficiently as a non-growable array.

Fields Summary
private int[]
nums
private transient int
size
Constructors Summary
Methods Summary
public voidadd(int x)
Add an int to the array, growing the array if necessary

    if (nums.length == size) resize(nums.length*2); // Grow array, if needed.
    nums[size++] = x;                               // Store the int in it.
  
public intelementAt(int index)
Return an element of the array

  // Index of next unused element of nums[].

         
        
    if (index >= size) throw new ArrayIndexOutOfBoundsException(index);
    else return nums[index];
  
private voidreadObject(java.io.ObjectInputStream in)
Compute the transient size field after deserializing the array

    in.defaultReadObject();                // Read the array normally.
    size = nums.length;                    // Restore the transient field.
  
protected voidresize(int newsize)
An internal method to change the allocated size of the array

    int[] oldnums = nums;
    nums = new int[newsize];                     // Create a new array.
    System.arraycopy(oldnums, 0, nums, 0, size); // Copy array elements.
  
private voidwriteObject(java.io.ObjectOutputStream out)
Get rid of unused array elements before serializing the array

    if (nums.length > size) resize(size);  // Compact the array.
    out.defaultWriteObject();              // Then write it out normally.