tl.lin.data.array
Class ArrayListOfInts

java.lang.Object
  extended by tl.lin.data.array.ArrayListOfInts
All Implemented Interfaces:
Cloneable, Iterable<Integer>, RandomAccess
Direct Known Subclasses:
ArrayListOfIntsWritable

public class ArrayListOfInts
extends Object
implements RandomAccess, Cloneable, Iterable<Integer>

Object representing a list of ints, backed by an resizable-array.


Field Summary
protected  int[] array
           
protected  int size
           
 
Constructor Summary
ArrayListOfInts()
          Constructs an empty list with an initial capacity of ten.
ArrayListOfInts(int initialCapacity)
          Constructs an empty list with the specified initial capacity.
ArrayListOfInts(int[] a)
          Constructs a list from an array.
ArrayListOfInts(int first, int last)
          Constructs a list populated with ints in range [first, last).
 
Method Summary
 ArrayListOfInts add(int e)
          Appends the specified element to the end of this list.
 ArrayListOfInts add(int index, int element)
          Inserts the specified element at the specified position in this list.
 void addUnique(int[] arr)
          Add all ints in the specified array into this list, ignoring duplicates.
 void clear()
          Removes all of the elements from this list.
 ArrayListOfInts clone()
          Returns a clone of this object.
 boolean contains(int n)
          Returns true if this list contains the specified element.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this object, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
 boolean equals(Object obj)
          Elementwise comparison.
 int get(int index)
          Returns the element at the specified position in this list.
 int[] getArray()
          Returns the array backing this object.
 int indexOf(int n)
          Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
 ArrayListOfInts intersection(ArrayListOfInts other)
          Computes the intersection of two sorted lists of unique ints.
 boolean isEmpty()
          Returns true if this list contains no elements.
 Iterator<Integer> iterator()
          Returns an iterator for this list.
 int lastIndexOf(int n)
          Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
 ArrayListOfInts merge(ArrayListOfInts sortedLst)
          Merges two sorted (ascending order) lists into one sorted union.
 ArrayListOfInts mergeNoDuplicates(ArrayListOfInts sortedLst)
          Merges two sorted (ascending order) lists into one sorted union.
 int remove(int index)
          Removes the element at the specified position in this list.
 int set(int index, int element)
          Replaces the element at the specified position in this list with the specified element.
 void setSize(int sz)
          Specifies the length of this list.
 void shiftLastNToTop(int n)
           
 int size()
          Returns the number of elements in this list.
 void sort()
          Sorts this list.
 ArrayListOfInts subList(int start, int end)
          Extracts a sub-list.
 String toString()
           
 String toString(int n)
          Returns a string representation of the object, explicitly printing out the first n elements of this list.
 void trimToSize()
          Trims the capacity of this object to be the list's current size.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

array

protected transient int[] array

size

protected int size
Constructor Detail

ArrayListOfInts

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

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

ArrayListOfInts

public ArrayListOfInts()
Constructs an empty list with an initial capacity of ten.


ArrayListOfInts

public ArrayListOfInts(int[] a)
Constructs a list from an array. Defensively makes a copy of the array.

Parameters:
a - source array

ArrayListOfInts

public ArrayListOfInts(int first,
                       int last)
Constructs a list populated with ints in range [first, last).

Parameters:
first - the smallest int in the range (inclusive)
last - the largest int in the range (exclusive)
Method Detail

trimToSize

public void trimToSize()
Trims the capacity of this object to be the list's current size. An application can use this operation to minimize the memory footprint of this object.


ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this object, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity

size

public int size()
Returns the number of elements in this list.


setSize

public void setSize(int sz)
Specifies the length of this list.


isEmpty

public boolean isEmpty()
Returns true if this list contains no elements.


contains

public boolean contains(int n)
Returns true if this list contains the specified element.

Parameters:
n - element whose presence in this list is to be tested
Returns:
true if this list contains the specified element

indexOf

public int indexOf(int n)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.


lastIndexOf

public int lastIndexOf(int n)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.


clone

public ArrayListOfInts clone()
Returns a clone of this object.

Overrides:
clone in class Object

get

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

Parameters:
index - index of the element to return
Returns:
the element at the specified position in this list

set

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

Parameters:
index - index of the element to replace
element - element to be stored at the specified position
Returns:
the element previously at the specified position

add

public ArrayListOfInts add(int e)
Appends the specified element to the end of this list.


add

public ArrayListOfInts add(int index,
                           int 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).

Parameters:
index - index at which the specified element is to be inserted
element - element to be inserted

remove

public int remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Parameters:
index - the index of the element to be removed
Returns:
the element that was removed from the list

clear

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


getArray

public int[] getArray()
Returns the array backing this object. Note that this array may be longer than the number of elements in the list.

Returns:
array backing this object

iterator

public Iterator<Integer> iterator()
Returns an iterator for this list. Note that this method is included only for convenience to conform to the Iterable interface; this method is not efficient because of autoboxing.

Specified by:
iterator in interface Iterable<Integer>

toString

public String toString(int n)
Returns a string representation of the object, explicitly printing out the first n elements of this list.


toString

public String toString()
Overrides:
toString in class Object

sort

public void sort()
Sorts this list.


intersection

public ArrayListOfInts intersection(ArrayListOfInts other)
Computes the intersection of two sorted lists of unique ints.

Parameters:
other - other list to be intersected with this list
Returns:
intersection of the two lists

merge

public ArrayListOfInts merge(ArrayListOfInts sortedLst)
Merges two sorted (ascending order) lists into one sorted union. Duplicate items remain in the merged list.

Parameters:
sortedLst - list to be merged into this
Returns:
merged sorted (ascending order) union of this and sortedLst

mergeNoDuplicates

public ArrayListOfInts mergeNoDuplicates(ArrayListOfInts sortedLst)
Merges two sorted (ascending order) lists into one sorted union. Duplicate items are discarded in the merged list.

Parameters:
sortedLst - list to be merged into this
Returns:
merged sorted (ascending order) union of this and sortedLst

subList

public ArrayListOfInts subList(int start,
                               int end)
Extracts a sub-list.

Parameters:
start - first index to be included in sub-list
end - last index to be included in sub-list
Returns:
a new ArrayListOfInts from start to end

addUnique

public void addUnique(int[] arr)
Add all ints in the specified array into this list, ignoring duplicates.

Parameters:
arr - array of ints to add to this object

shiftLastNToTop

public void shiftLastNToTop(int n)

equals

public boolean equals(Object obj)
Elementwise comparison. Shorter always comes before if it is a sublist of longer. No preference if both are empty.

Overrides:
equals in class Object
Parameters:
obj - other object this is compared against


Copyright © 2013. All Rights Reserved.