Top Description Inners Fields Constructors Methods
com.google.common.primitives

public final Class Ints

extends IntsMethodsForWeb
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkElementIndex, .Preconditions.checkNotNull, .Preconditions.checkPositionIndexes

Static utility methods pertaining to int primitives, that are not already found in either Integer or Arrays.

See the Guava User Guide article on primitive utilities.

Author
Kevin Bourrillion
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static enum

Field Summary

Modifier and TypeField and Description
public static final int
BYTES

The number of bytes required to represent a primitive int value.

public static final int
MAX_POWER_OF_TWO

The largest power of two that can be represented as an int.

Constructor Summary

AccessConstructor and Description
private
Ints()

Method Summary

Modifier and TypeMethod and Description
public static List<Integer>

Returns:

a list view of the array
asList
(int...
the array to back the list
backingArray
)

Returns a fixed-size list backed by the specified array, similar to Arrays#asList(Object[]).

public static int

Returns:

the int value that equals value
checkedCast
(long
any value in the range of the int type
value
)

Returns the int value that is equal to value, if possible.

public static int

Returns:

a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
compare
(int
the first int to compare
a
,
int
the second int to compare
b
)

Compares the two specified int values.

public static int[]

Returns:

a single array containing all the values from the source arrays, in order
concat
(int[]...
zero or more int arrays
arrays
)

Returns the values from each provided array combined into a single array.

public static int
constrainToRange(int
the int value to constrain
value
,
int
the lower bound (inclusive) of the range to constrain value to
min
,
int
the upper bound (inclusive) of the range to constrain value to
max
)

Returns the value nearest to value which is within the closed range [min..max].

public static boolean

Returns:

true if array[i] == target for some value of i
contains
(int[]
an array of int values, possibly empty
array
,
int
a primitive int value
target
)

Returns true if target is present as an element anywhere in array.

public static int[]

Returns:

an array containing the values of array, with guaranteed minimum length minLength
ensureCapacity
(int[]
the source array
array
,
int
the minimum length the returned array must guarantee
minLength
,
int
an extra amount to "grow" the array by if growth is necessary
padding
)

Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.

public static int
fromByteArray(byte[] bytes)

Returns the int value whose big-endian representation is stored in the first 4 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getInt().

public static int
fromBytes(byte b1, byte b2, byte b3, byte b4)

Returns the int value whose byte representation is the given 4 bytes, in big-endian order; equivalent to Ints.fromByteArray(new byte[] {b1, b2, b3, b4}).

public static int

Returns:

a hash code for the value
hashCode
(int
a primitive int value
value
)

Returns a hash code for value; equal to the result of invoking ((Integer) value).hashCode().

public static int

Returns:

the least index i for which array[i] == target, or -1 if no such index exists.
indexOf
(int[]
an array of int values, possibly empty
array
,
int
a primitive int value
target
)

Returns the index of the first appearance of the value target in array.

private static int
indexOf(int[] array, int target, int start, int end)

public static int
indexOf(int[]
the array to search for the sequence target
array
,
int[]
the array to search for as a sub-sequence of array
target
)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

public static String
join(String
the text that should appear between consecutive values in the resulting string (but not at the start or end)
separator
,
int...
an array of int values, possibly empty
array
)

Returns a string containing the supplied int values separated by separator.

public static int

Returns:

the greatest index i for which array[i] == target, or -1 if no such index exists.
lastIndexOf
(int[]
an array of int values, possibly empty
array
,
int
a primitive int value
target
)

Returns the index of the last appearance of the value target in array.

private static int
lastIndexOf(int[] array, int target, int start, int end)

public static Comparator<int[]>
lexicographicalComparator()

Returns a comparator that compares two int arrays lexicographically.

public static int

Returns:

the value present in array that is greater than or equal to every other value in the array
max
(int...
a nonempty array of int values
array
)

Returns the greatest value present in array.

public static int

Returns:

the value present in array that is less than or equal to every other value in the array
min
(int...
a nonempty array of int values
array
)

Returns the least value present in array.

public static void
reverse(int[] array)

Reverses the elements of array.

public static void
reverse(int[] array, int fromIndex, int toIndex)

Reverses the elements of array between fromIndex inclusive and toIndex exclusive.

public static void
rotate(int[] array, int distance)

Performs a right rotation of array of "distance" places, so that the first element is moved to index "distance", and the element at index i ends up at index (distance + i) mod array.length.

public static void
rotate(int[] array, int distance, int fromIndex, int toIndex)

Performs a right rotation of array between fromIndex inclusive and toIndex exclusive.

public static int

Returns:

the same value cast to int if it is in the range of the int type, Integer#MAX_VALUE if it is too large, or Integer#MIN_VALUE if it is too small
saturatedCast
(long
any long value
value
)

Returns the int nearest in value to value.

public static void
sortDescending(int[] array)

Sorts the elements of array in descending order.

public static void
sortDescending(int[] array, int fromIndex, int toIndex)

Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.

public static Converter<String, Integer>
stringConverter()

Returns a serializable converter object that converts between strings and integers using Integer#decode and Integer#toString().

public static int[]

Returns:

an array containing the same values as collection, in the same order, converted to primitives
toArray
(Collection<? extends Number>
a collection of Number instances
collection
)

Returns an array containing each value of collection, converted to a int value in the manner of Number#intValue.

public static byte[]
toByteArray(int value)

Returns a big-endian representation of value in a 4-element byte array; equivalent to ByteBuffer.allocate(4).putInt(value).array().

public static Integer

Returns:

the integer value represented by string, or null if string has a length of zero or cannot be parsed as an integer value
tryParse
(String
the string representation of an integer value
string
)

Parses the specified string as a signed decimal integer value.

public static Integer

Returns:

the integer value represented by string using radix, or null if string has a length of zero or cannot be parsed as an integer value
tryParse
(String
the string representation of an integer value
string
,
int
the radix to use when parsing
radix
)

Parses the specified string as a signed integer value using the specified radix.