APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.utils

public Class IntArray

extends Object
Class Inheritance
Imports
com.badlogic.gdx.math.MathUtils, java.util.Arrays

A resizable, ordered or unordered int array. Avoids the boxing that occurs with ArrayList. If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
Author
Nathan Sweet

Field Summary

Modifier and TypeField and Description
public int[]
public boolean
public int

Constructor Summary

AccessConstructor and Description
public
IntArray()

Creates an ordered array with a capacity of 16.

public
IntArray(int capacity)

Creates an ordered array with the specified capacity.

public
IntArray(boolean
If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
ordered
,
int
Any elements added beyond this will cause the backing array to be grown.
capacity
)

public
IntArray(IntArray array)

Creates a new array containing the elements in the specific array.

public
IntArray(int[] array)

Creates a new ordered array containing the elements in the specified array.

public
IntArray(boolean
If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
ordered
,
int[] array, int startIndex, int count)

Creates a new array containing the elements in the specified array.

Method Summary

Modifier and TypeMethod and Description
public void
add(int value)

public void
add(int value1, int value2)

public void
add(int value1, int value2, int value3)

public void
add(int value1, int value2, int value3, int value4)

public void
public void
addAll(IntArray array, int offset, int length)

public void
addAll(int... array)

public void
addAll(int[] array, int offset, int length)

public void
public boolean
contains(int value)

public int[]

Returns:

items
ensureCapacity
(int additionalCapacity)

Increases the size of the backing array to accommodate the specified number of additional items.

public boolean
equals(Object object)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
first()

Returns the first item.

public int
get(int index)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public void
incr(int index, int value)

public void
incr(int value)

public int
indexOf(int value)

public void
insert(int index, int value)

public void
insertRange(int index, int count)

Inserts the specified number of items at the specified index.

public boolean
isEmpty()

Returns true if the array is empty.

public int
lastIndexOf(int value)

public void
mul(int index, int value)

public void
mul(int value)

public boolean
notEmpty()

Returns true if the array has one or more items.

public int
peek()

Returns the last item.

public int
pop()

Removes and returns the last item.

public int
random()

Returns a random item from the array, or zero if the array is empty.

public boolean

Returns:

true if this array was modified.
removeAll
(IntArray array)

Removes from this array the first instance of each element contained in the specified array.

public int
removeIndex(int index)

Removes and returns the item at the specified index.

public void
removeRange(int start, int end)

Removes the items between the specified indices, inclusive.

public boolean
removeValue(int value)

public int

Returns:

the number of replacements done
replaceAll
(int
the int to be replaced
value
,
int
all occurrences of value will be replaced by this replacement
replacement
)

Returns the number of replacements done.

public boolean

Returns:

if value was found and replaced
replaceFirst
(int
the int to be replaced
value
,
int
the first value will be replaced by this replacement if found
replacement
)

Returns true if the specified value was replaced successfully with the replacement

protected int[]
resize(int newSize)

public void
public void
set(int index, int value)

public int[]

Returns:

items
setSize
(int newSize)

Sets the array size, leaving any values beyond the current size undefined.

public int[]

Returns:

items
shrink
()

Reduces the size of the backing array to the size of the actual items.

public void
public void
sort()

public void
swap(int first, int second)

public int[]
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public String
toString(String separator)

public void
truncate(int newSize)

Reduces the size of the array to the specified size.

public static IntArray
with(int... array)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

itemsback to summary
public int[] items
orderedback to summary
public boolean ordered
sizeback to summary
public int size

Constructor Detail

IntArrayback to summary
public IntArray()

Creates an ordered array with a capacity of 16.

IntArrayback to summary
public IntArray(int capacity)

Creates an ordered array with the specified capacity.

IntArrayback to summary
public IntArray(boolean ordered, int capacity)
Parameters
ordered:boolean

If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.

capacity:int

Any elements added beyond this will cause the backing array to be grown.

IntArrayback to summary
public IntArray(IntArray array)

Creates a new array containing the elements in the specific array. The new array will be ordered if the specific array is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.

IntArrayback to summary
public IntArray(int[] array)

Creates a new ordered array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.

IntArrayback to summary
public IntArray(boolean ordered, int[] array, int startIndex, int count)

Creates a new array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.

Parameters
ordered:boolean

If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.

Method Detail

addback to summary
public void add(int value)
addback to summary
public void add(int value1, int value2)
addback to summary
public void add(int value1, int value2, int value3)
addback to summary
public void add(int value1, int value2, int value3, int value4)
addAllback to summary
public void addAll(IntArray array)
addAllback to summary
public void addAll(IntArray array, int offset, int length)
addAllback to summary
public void addAll(int... array)
addAllback to summary
public void addAll(int[] array, int offset, int length)
clearback to summary
public void clear()
containsback to summary
public boolean contains(int value)
ensureCapacityback to summary
public int[] ensureCapacity(int additionalCapacity)

Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.

Returns:int[]

items

equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

firstback to summary
public int first()

Returns the first item.

getback to summary
public int get(int index)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
incrback to summary
public void incr(int index, int value)
incrback to summary
public void incr(int value)
indexOfback to summary
public int indexOf(int value)
insertback to summary
public void insert(int index, int value)
insertRangeback to summary
public void insertRange(int index, int count)

Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion.

isEmptyback to summary
public boolean isEmpty()

Returns true if the array is empty.

lastIndexOfback to summary
public int lastIndexOf(int value)
mulback to summary
public void mul(int index, int value)
mulback to summary
public void mul(int value)
notEmptyback to summary
public boolean notEmpty()

Returns true if the array has one or more items.

peekback to summary
public int peek()

Returns the last item.

popback to summary
public int pop()

Removes and returns the last item.

randomback to summary
public int random()

Returns a random item from the array, or zero if the array is empty.

removeAllback to summary
public boolean removeAll(IntArray array)

Removes from this array the first instance of each element contained in the specified array.

Returns:boolean

true if this array was modified.

removeIndexback to summary
public int removeIndex(int index)

Removes and returns the item at the specified index.

removeRangeback to summary
public void removeRange(int start, int end)

Removes the items between the specified indices, inclusive.

removeValueback to summary
public boolean removeValue(int value)
replaceAllback to summary
public int replaceAll(int value, int replacement)

Returns the number of replacements done.

Parameters
value:int

the int to be replaced

replacement:int

all occurrences of value will be replaced by this replacement

Returns:int

the number of replacements done

replaceFirstback to summary
public boolean replaceFirst(int value, int replacement)

Returns true if the specified value was replaced successfully with the replacement

Parameters
value:int

the int to be replaced

replacement:int

the first value will be replaced by this replacement if found

Returns:boolean

if value was found and replaced

resizeback to summary
protected int[] resize(int newSize)
reverseback to summary
public void reverse()
setback to summary
public void set(int index, int value)
setSizeback to summary
public int[] setSize(int newSize)

Sets the array size, leaving any values beyond the current size undefined.

Returns:int[]

items

shrinkback to summary
public int[] shrink()

Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.

Returns:int[]

items

shuffleback to summary
public void shuffle()
sortback to summary
public void sort()
swapback to summary
public void swap(int first, int second)
toArrayback to summary
public int[] toArray()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

toStringback to summary
public String toString(String separator)
truncateback to summary
public void truncate(int newSize)

Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken.

withback to summary
public static IntArray with(int... array)
See Also
IntArray(int[])