Package ints
Class IntList
- java.lang.Object
-
- ints.IntList
-
public class IntList extends java.lang.Object
Class
ClassIntList
represents a list of integers. ClassIntList
supports aclear()
method, but it does not support aremove()
method.IntList
is not thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_INIT_CAPACITY
The default initial capacity of anIntList
, which is 16.
-
Constructor Summary
Constructors Constructor Description IntList()
Constructs anIntList
object with the default initial capacity.IntList(int initCapacity)
Constructs anIntList
object with the specified initial capacity.IntList(int[] ia)
Constructs anIntList
by cloning the specified array.IntList(IntList intList)
Constructs anIntList
by copying the specifiedIntList
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int value)
Adds the specified integer to the end of this list.int
binarySearch(int value)
Returns an index associated with the specified value, or(-insertionPoint - 1)
if the value is not found.void
clear()
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
.int
decrementAndGet(int index)
Decrements by one the element at the specified position in this list.int
get(int index)
Returns the element at the specified position in this list.int
getAndDecrement(int index)
Decrements by one the element at the specified position in this list.int
getAndIncrement(int index)
Increments by one the element at the specified position in this list.int
incrementAndGet(int index)
Increments by one the element at the specified position in this list.boolean
isEmpty()
Returnstrue
if this list has no elements, and returnsfalse
otherwise.int
pop()
Removes and returns the last entry of this list.int
set(int index, int value)
Replaces the element at the specified position in this list with the specified element.int
size()
Returns the number of elements in this list.void
sort()
Sorts the elements of this list in increasing orderjava.util.stream.IntStream
stream()
Returns anIntStream
containing the sequence of elements in this list.int[]
toArray()
Returns an integer array containing the sequence of elements in this list.java.lang.String
toString()
Returnsjava.util.Arrays.toString(this.toArray())
void
truncate(int newSize)
Truncates this list of integer by removing all elements whose index is greater than or equal to the specified size.
-
-
-
Field Detail
-
DEFAULT_INIT_CAPACITY
public static final int DEFAULT_INIT_CAPACITY
The default initial capacity of anIntList
, which is 16.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
IntList
public IntList()
Constructs anIntList
object with the default initial capacity.- See Also:
DEFAULT_INIT_CAPACITY
-
IntList
public IntList(int initCapacity)
Constructs anIntList
object with the specified initial capacity.- Parameters:
initCapacity
- the initial capacity of this list- Throws:
java.lang.IllegalArgumentException
- ifinitCapacity < 0
-
IntList
public IntList(int[] ia)
Constructs anIntList
by cloning the specified array.- Parameters:
ia
- a list of integer values- Throws:
java.lang.NullPointerException
- ifia == null
-
IntList
public IntList(IntList intList)
Constructs anIntList
by copying the specifiedIntList
.- Parameters:
intList
- a list of integer values- Throws:
java.lang.NullPointerException
- ifintList == null
-
-
Method Detail
-
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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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()
Returnstrue
if this list has no elements, and returnsfalse
otherwise.- Returns:
true
if 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:
java.lang.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:
java.lang.IndexOutOfBoundsException
- ifstart < 0 || start > this.size()
java.lang.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:
java.lang.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
public java.util.stream.IntStream stream()
Returns anIntStream
containing 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
IntStream
containing the sequence of elements in this list
-
clear
public void clear()
Removes all elements from this list.
-
toString
public java.lang.String toString()
Returnsjava.util.Arrays.toString(this.toArray())
- Overrides:
toString
in classjava.lang.Object
- Returns:
java.util.Arrays.toString(this.toArray())
-
-