Package ints
Class IntList
java.lang.Object
ints.IntList
Class IntList represents a list of integers.
Class IntList supports a clear() method, but it does not
support a remove() method.
IntList is not thread-safe.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default initial capacity of anIntList, which is 16. -
Constructor Summary
ConstructorsConstructorDescriptionIntList()Constructs anIntListobject with the default initial capacity.IntList(int initCapacity) Constructs anIntListobject with the specified initial capacity.IntList(int[] ia) Constructs anIntListby cloning the specified array.Constructs anIntListby copying the specifiedIntList. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int value) Adds the specified integer to the end of this list.intbinarySearch(int value) Returns an index associated with the specified value, or(-insertionPoint - 1)if the value is not found.voidclear()Removes all elements from this list.int[]copyOf(int newLength) Copies and returns this list of integers, truncating or padding with 0 as necessary so that the copy has the specified length.int[]copyOfRange(int start, int end) Returns the specified range of elements adding 0 values to the end of the copied values if necessary so that the returned array has lengthend - start.intdecrementAndGet(int index) Decrements by one the element at the specified position in this list.intget(int index) Returns the element at the specified position in this list.intgetAndDecrement(int index) Decrements by one the element at the specified position in this list.intgetAndIncrement(int index) Increments by one the element at the specified position in this list.intincrementAndGet(int index) Increments by one the element at the specified position in this list.booleanisEmpty()Returnstrueif this list has no elements, and returnsfalseotherwise.intpop()Removes and returns the last entry of this list.intset(int index, int value) Replaces the element at the specified position in this list with the specified element.intsize()Returns the number of elements in this list.voidsort()Sorts the elements of this list in increasing orderstream()Returns anIntStreamcontaining the sequence of elements in this list.int[]toArray()Returns an integer array containing the sequence of elements in this list.toString()Returnsjava.util.Arrays.toString(this.toArray())voidtruncate(int newSize) Truncates this list of integer by removing all elements whose index is greater than or equal to the specified size.
-
Field Details
-
DEFAULT_INIT_CAPACITY
public static final int DEFAULT_INIT_CAPACITYThe default initial capacity of anIntList, which is 16.- See Also:
-
-
Constructor Details
-
IntList
public IntList()Constructs anIntListobject with the default initial capacity.- See Also:
-
IntList
public IntList(int initCapacity) Constructs anIntListobject with the specified initial capacity.- Parameters:
initCapacity- the initial capacity of this list- Throws:
IllegalArgumentException- ifinitCapacity < 0
-
IntList
public IntList(int[] ia) Constructs anIntListby cloning the specified array.- Parameters:
ia- a list of integer values- Throws:
NullPointerException- ifia == null
-
IntList
Constructs anIntListby copying the specifiedIntList.- Parameters:
intList- a list of integer values- Throws:
NullPointerException- ifintList == null
-
-
Method Details
-
add
public void add(int value) Adds the specified integer to the end of this list.- Parameters:
value- the integer to be added to the end of this list
-
pop
public int pop()Removes and returns the last entry of this list.- Returns:
- the last entry of this list
- Throws:
IndexOutOfBoundsException- ifthis.isEmpty() == true
-
get
public int get(int index) Returns the element at the specified position in this list.- Parameters:
index- the index of the element to be returned- Returns:
- the element at the specified position in this list
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
set
public int set(int index, int value) Replaces the element at the specified position in this list with the specified element.- Parameters:
index- the index of the element to be replacedvalue- the value to be stored at the specified position in this list- Returns:
- the previous value at the specified position in this list
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
getAndIncrement
public int getAndIncrement(int index) Increments by one the element at the specified position in this list.- Parameters:
index- the index of the element to be incremented- Returns:
- the previous element at the specified position in this list
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
getAndDecrement
public int getAndDecrement(int index) Decrements by one the element at the specified position in this list.- Parameters:
index- the index of the element to be decremented- Returns:
- the previous element at the specified position in this list
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
incrementAndGet
public int incrementAndGet(int index) Increments by one the element at the specified position in this list.- Parameters:
index- the index of the element to be incremented- Returns:
- the updated element at the specified position in this list
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
decrementAndGet
public int decrementAndGet(int index) Decrements by one the element at the specified position in this list.- Parameters:
index- the index of the element to be decremented- Returns:
- the updated element at the specified position in this list
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
size
public int size()Returns the number of elements in this list.- Returns:
- the number of elements in this list
-
isEmpty
public boolean isEmpty()Returnstrueif this list has no elements, and returnsfalseotherwise.- Returns:
trueif this list has no elements
-
sort
public void sort()Sorts the elements of this list in increasing order -
binarySearch
public int binarySearch(int value) Returns an index associated with the specified value, or(-insertionPoint - 1)if the value is not found. The returned value is undefined if the list of integers is not sorted in increasing order.- Parameters:
value- the value to be searched for- Returns:
- an index associated with the specified value, or
(-insertionPoint - 1)if the value is not found
-
copyOf
public int[] copyOf(int newLength) Copies and returns this list of integers, truncating or padding with 0 as necessary so that the copy has the specified length.- Parameters:
newLength- the length of the returned array- Returns:
- a copy of this list of integers, truncating or padding with 0 as necessary so that the copy has the specified length
- Throws:
NegativeArraySizeException- ifnewLength < 0
-
copyOfRange
public int[] copyOfRange(int start, int end) Returns the specified range of elements adding 0 values to the end of the copied values if necessary so that the returned array has lengthend - start.- Parameters:
start- the start of the range to be copied (inclusive)end- the end of the range to be copied (exclusive)- Returns:
- the specified range of elements
- Throws:
IndexOutOfBoundsException- ifstart < 0 || start > this.size()NegativeArraySizeException- ifend > start
-
truncate
public void truncate(int newSize) Truncates this list of integer by removing all elements whose index is greater than or equal to the specified size. The list is unchanged if the specified size is greater than the number of elements in the list.- Parameters:
newSize- the number of elements in the truncated list- Throws:
IllegalArgumentException- ifnewSize < 0
-
toArray
public int[] toArray()Returns an integer array containing the sequence of elements in this list.- Returns:
- an integer array containing the sequence of elements in this list
-
stream
Returns anIntStreamcontaining the sequence of elements in this list. The contract for this method is unspecified if this list is modified during use of the returned stream.- Returns:
- an
IntStreamcontaining the sequence of elements in this list
-
clear
public void clear()Removes all elements from this list. -
toString
Returnsjava.util.Arrays.toString(this.toArray())
-