Arrays All Versions
This draft deletes the entire topic.
Introduction
int
, or reference types like Object
.Examples
-
Basic cases
int[] numbers1 = new int[3]; // Array for 3 int values, default value is 0 int[] numbers2 = { 1, 2, 3 }; // Array literal of 3 int values int[] numbers3 = new int[] { 1, 2, 3 }; // Array of 3 int values initialized int[][] numbers4 = { { 1, 2 }, { 3, 4, 5 } }; // Jagged array literal int[][] numbers5 = new int[5][]; // Jagged array, one dimension 5 long int[][] numbers6 = new int[5][4]; // Multidimensional array: 5x4
Arrays may be created using any primitive or reference type.
float[] boats = new float[5]; // Array of five 32-bit floating point numbers. double[] header = new double[] { 4.56, 332.267, 7.0, 0.3367, 10.0 }; // Array of five 64-bit floating point numbers. String[] theory = new String[] { "a", "b", "c" }; // Array of three strings (reference type). Object[] dArt = new Object[] { new Object(), "We love Stack Overflow.", new Integer(3) }; // Array of three Objects (reference type).
For the last example, note that subtypes of the declared array type are allowed in the array.
Arrays, Collections, and Streams
Java SE 1.2// Parameters require objects, not primitives // Auto-boxing happening for int 127 here Integer[] initial = { 127, Integer.valueOf( 42 ) }; List<Integer> toList = Arrays.asList( initial ); // Fixed size! // Note: Works with all collections Integer[] fromCollection = toList.toArray( new Integer[toList.size()] ); //Java doesn't allow you to create an array of a parameterized type List<String>[] list = new ArrayList<String>[2]; // Compilation error!
Java SE 8// Streams - JDK 8+ Stream<Integer> toStream = Arrays.stream( initial ); Integer[] fromStream = toStream.toArray( Integer[]::new );
Intro
An array is a data structure that holds a fixed number of primitive values or references to object instances.
Each item in an array is called an element, and each element is accessed by its numerical index. The length of an array is established when the array is created:
int size = 42; int[] array = new int[size];
The size of an array is fixed at runtime when initialized. It cannot be changed after initialization. If the size must be mutable at runtime, a
Collection
class such asArrayList
should be used instead.ArrayList
stores elements in an array and supports resizing by allocating a new array and copying elements from the old array.If the array is of a primitive type, i.e.
int[] array1 = { 1,2,3 }; int[] array2 = new int[10];
the values are stored in the array itself. In the absence of an initializer (as in
array2
above), the default value assigned to each element is0
(zero).If the array type is an object reference, as in
SomeClassOrInterface[] array = new SomeClassOrInterface[10];
then the array contains references to objects of type
SomeClassOrInterface
. Those references can refer to an instance ofSomeClassOrInterface
or any subclass (for classes) or implementing class (for interfaces) ofSomeClassOrInterface
. If the array declaration has no initializer then the default value ofnull
is assigned to each element.Because all arrays are
int
-indexed, the size of an array must be specified by anint
. The size of the array cannot be specified as along
:long size = 23L; int[] array = new int[size]; // Compile-time error: // incompatible types: possible lossy conversion from // long to int
Arrays use a zero-based index system, which means indexing starts at
0
and ends atlength - 1
.For example, the following image represents an array with size
10
. Here, the first element is at index0
and the last element is at index9
, instead of the first element being at index1
and the last element at index10
(see figure below).Java offers several ways of defining and initializing arrays, including literal and constructor notations. When declaring arrays using the
new Type[length]
constructor, each element will be initialized with the following default values:0
for primitive numerical types:byte
,short
,int
,long
,float
, anddouble
.'\u0000'
(null character) for thechar
type.false
for theboolean
type.null
for reference types.
Creating and initializing primitive type arrays
int[] array1 = new int[] { 1, 2, 3 }; // Create an array with new operator and // array initializer. int[] array2 = { 1, 2, 3 }; // Shortcut syntax with array initializer. int[] array3 = new int[3]; // Equivalent to { 0, 0, 0 } int[] array4 = null; // The array itself is an object, so it // can be set as null.
When declaring an array,
[]
will appear as part of the type at the beginning of the declaration (after the type name), or as part of the declarator for a particular variable (after variable name), or both:int array5[]; /* equivalent to */ int[] array5; int a, b[], c[][]; /* equivalent to */ int a; int[] b; int[][] c; int[] a, b[]; /* equivalent to */ int[] a; int[][] b; int a, []b, c[][]; /* Compilation Error, because [] is not part of the type at beginning of the declaration, rather it is before 'b'. */ // The same rules apply when declaring a method that returns an array: int foo()[] { ... } /* equivalent to */ int[] foo() { ... }
In the following example, both declarations are correct and can compile and run without any problems. However, both the Java Coding Convention and the Google Java Style Guide discourage the form with brackets after the variable name—the brackets identify the array type and should appear with the type designation. The same should be used for method return signatures.
float array[]; /* and */ int foo()[] { ... } /* are discouraged */ float[] array; /* and */ int[] foo() { ... } /* are encouraged */
The discouraged type is meant to accommodate transitioning C users, who are familiar with the syntax for C which has the brackets after the variable name.
In Java, it is possible to have arrays of size
0
:int[] array = new int[0]; // Compiles and runs fine. int array2 = {}; // Equivalent syntax.
However, since it's an empty array, no elements can be read from it or assigned to it:
array[0] = 1; // Throws java.lang.ArrayIndexOutOfBoundsException. int i = array2[0]; // Also throws ArrayIndexOutOfBoundsException.
Such empty arrays are typically useful as return values, so that the calling code only has to worry about dealing with an array, rather than a potential
null
value that may lead to aNullPointerException
.The length of an array must be a non-negative integer:
int[] array = new int[-1]; // Throws java.lang.NegativeArraySizeException
The array size can be determined using a public final field called
length
:System.out.println(array.length); // Prints 0 in this case.
Note:
array.length
returns the actual size of the array and not the number of array elements which were assigned a value, unlikeArrayList#size()
which returns the number of array elements which were assigned a value.Creating and initializing multi-dimensional arrays
The simplest way to create a multi-dimensional array is as follows:
int[][] a = new int[2][3];
It will create two three-length
int
arrays—a[0]
anda[1]
. This is very similar to the classical, C-style initialization of rectangular multi-dimensional arrays.You can create and initialize at the same time:
int[][] a = { {1, 2}, {3, 4}, {5, 6} };
Unlike C, where only rectangular multi-dimensional arrays are supported, inner arrays do not need to be of the same length, or even defined:
int[][] a = { {1}, {2, 3}, null };
Here,
a[0]
is a one-lengthint
array, whereasa[1]
is a two-lengthint
array anda[2]
isnull
. Arrays like this are called jagged arrays or ragged arrays, that is, they are arrays of arrays. Multi-dimensional arrays in Java are implemented as arrays of arrays, i.e.array[i][j][k]
is equivalent to((array[i])[j])[k]
. Unlike C#, the syntaxarray[i,j]
is not supported in Java.Multidimensional array representation in Java
Creating and initializing reference type arrays
String[] array6 = new String[] { "Laurel", "Hardy" }; // Create an array with new // operator and array initializer. String[] array7 = { "Laurel", "Hardy" }; // Shortcut syntax with array // initializer. String[] array8 = new String[3]; // { null, null, null } String[] array9 = null; // null
In addition to the
String
literals and primitives shown above, the shortcut syntax for array initialization also works with canonicalObject
types:Object[] array10 = { new Object(), new Object() };
Because arrays are covariant, a reference type array can be initialized as an array of a subclass, although an
ArrayStoreException
will be thrown if you try to set an element to something other than aString
:Object[] array11 = new String[] { "foo", "bar", "baz" }; array11[1] = "qux"; // fine array11[1] = new StringBuilder(); // throws ArrayStoreException
The shortcut syntax cannot be used for this because the shortcut syntax would have an implicit type of
Object[]
.An array can be initialized with zero elements by using
String[] emptyArray = new String[0]
. For example, an array with zero length like this is used for Creating anArray
from aCollection
when the method needs the runtime type of an object.In both primitive and reference types, an empty array initialization (for example
String[] array8 = new String[3]
) will initialize the array with the default value for each data type.Creating and initializing generic type arrays
In generic classes, arrays of generic types cannot be initialized like this due to type erasure:
public class MyGenericClass<T> { private T[] a; public MyGenericClass() { a = new T[5]; // Compile time error: generic array creation } }
Instead, they can be created using one of the following methods: (note that these will generate unchecked warnings)
-
By creating an
Object
array, and casting it to the generic type:a = (T[]) new Object[5];
This is the simplest method, but since the underlying array is still of type
Object[]
, this method does not provide type safety. Therefore, this method of creating an array is best used only within the generic class - not exposed publicly. -
By using
Array.newInstance
with a class parameter:public MyGenericClass(Class<T> clazz) { a = (T[]) Array.newInstance(clazz, 5); }
Here the class of
T
has to be explicitly passed to the constructor. The return type ofArray.newInstance
is alwaysObject
. However, this method is safer because the newly created array is always of typeT[]
, and therefore can be safely externalized.
Filling an array after initialization
Java SE 1.2Arrays.fill()
can be used to fill an array with the same value after initialization:Arrays.fill(array8, "abc"); // { "abc", "abc", "abc" }
fill()
can also assign a value to each element of the specified range of the array:Arrays.fill(array8, 1, 2, "aaa"); // Placing "aaa" from index 1 to 2.
Java SE 8Since Java version 8, the method
setAll
, and itsConcurrent
equivalentparallelSetAll
, can be used to set every element of an array to generated values. These methods are passed a generator function which accepts an index and returns the desired value for that position.The following example creates an integer array and sets all of its elements to their respective index value:
int[] array = new int[5]; Arrays.setAll(array, i -> i); // The array becomes { 0, 1, 2, 3, 4 }.
Separate declaration and initialization of arrays
The value of an index for an array element must be a whole number (0, 1, 2, 3, 4, ...) and less than the length of the array (indexes are zero-based). Otherwise, an ArrayIndexOutOfBoundsException will be thrown:
int[] array9; // Array declaration - uninitialized array9 = new int[3]; // Initialize array - { 0, 0, 0 } array9[0] = 10; // Set index 0 value - { 10, 0, 0 } array9[1] = 20; // Set index 1 value - { 10, 20, 0 } array9[2] = 30; // Set index 2 value - { 10, 20, 30 }
Arrays may not be re-initialized with array initializer shortcut syntax
It is not possible to re-initialize an array via a shortcut syntax with an array initializer since an array initializer can only be specified in a field declaration or local variable declaration, or as a part of an array creation expression.
However, it is possible to create a new array and assign it to the variable being used to reference the old array. While this results in the array referenced by that variable being re-initialized, the variable contents are a completely new array. To do this, the
new
operator can be used with an array initializer and assigned to the array variable:// First initialization of array int[] array = new int[] { 1, 2, 3 }; // Prints "1 2 3 ". for (int i : array) { System.out.print(i + " "); } // Re-initializes array to a new int[] array. array = new int[] { 4, 5, 6 }; // Prints "4 5 6 ". for (int i : array) { System.out.print(i + " "); } array = { 1, 2, 3, 4 }; // Compile-time error! Can't re-initialize an array via shortcut // syntax with array initializer.
-
The
Arrays.asList()
method can be used to return a fixed-sizeList
containing the elements of the given array. The resultingList
will be of the same parameter type as the base type of the array.String[] stringArray = {"foo", "bar", "baz"}; List<String> stringList = Arrays.asList(stringArray);
Note: This list is backed by (a view of) the original array, meaning that any changes to the list will change the array and vice versa. However, changes to the list that would change its size (and hence the array length) will throw an exception.
To create a copy of the list, use the constructor of
java.util.ArrayList
taking aCollection
:Java SE 5String[] stringArray = {"foo", "bar", "baz"}; List<String> stringList = new ArrayList<String>(Arrays.asList(stringArray));
Java SE 7In Java SE 7 and later, a pair of angle brackets
<>
(empty set of type arguments) can be used, which is called the Diamond Operator. The compiler can determine the type arguments from the context. This means the type information can be left out when calling the constructor ofArrayList
and it will be inferred automatically during compilation. This is called Type Inference which is a part of Java Generics.// Using Arrays.asList() String[] stringArray = {"foo", "bar", "baz"}; List<String> stringList = new ArrayList<>(Arrays.asList(stringArray)); // Using ArrayList.addAll() String[] stringArray = {"foo", "bar", "baz"}; ArrayList<String> list = new ArrayList<>(); list.addAll(Arrays.asList(stringArray)); // Using Collections.addAll() String[] stringArray = {"foo", "bar", "baz"}; ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, stringArray);
A point worth noting about the Diamond Operator is that it cannot be used with Anonymous Classes.
Java SE 8// Using Streams int[] ints = {1, 2, 3}; List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList()); String[] stringArray = {"foo", "bar", "baz"}; List<Object> list = Arrays.stream(stringArray).collect(Collectors.toList());
Important notes related to using Arrays.asList() method
-
This method returns
List
, which is an instance ofArrays$ArrayList
(static inner class ofArrays
) and notjava.util.ArrayList
. The resultingList
is of fixed-size. That means, adding or removing elements is not supported and will throw anUnsupportedOperationException
:stringList.add("something"); // throws java.lang.UnsupportedOperationException
-
A new
List
can be created by passing an array-backedList
to the constructor of a newList
. This creates a new copy of the data, which has changeable size and that is not backed by the original array:List<String> modifiableList = new ArrayList<>(Arrays.asList("foo", "bar"));
-
Calling
<T> List<T> asList(T... a)
on a primitive array, such as anint[]
, will produce aList<int[]>
whose only element is the source primitive array instead of the actual elements of the source array.The reason for this behavior is that primitive types cannot be used in place of generic type parameters, so the entire primitive array replaces the generic type parameter in this case. In order to convert a primitive array to a
List
, first of all, convert the primitive array to an array of the corresponding wrapper type (i.e. callArrays.asList
on anInteger[]
instead of anint[]
).Therefore, this will print
false
:int[] arr = {1, 2, 3}; // primitive array of int System.out.println(Arrays.asList(arr).contains(1));
On the other hand, this will print
true
:Integer[] arr = {1, 2, 3}; // object array of Integer (wrapper for int) System.out.println(Arrays.asList(arr).contains(1));
As well as this one (also will print
true
, because array will be interpreted as anInteger[]
):System.out.println(Arrays.asList(1,2,3).contains(1));
-
-
-
Two methods in
java.util.Collection
create an array from a collection:Object[] toArray()
can be used as follows:Java SE 5Set<String> set = new HashSet<String>(); set.add("red"); set.add("blue"); // although set is a Set<String>, toArray() returns an Object[] not a String[] Object[] objectArray = set.toArray();
<T> T[] toArray(T[] a)
can be used as follows:Java SE 5Set<String> set = new HashSet<String>(); set.add("red"); set.add("blue"); // The array does not need to be created up front with the correct size. // Only the array type matters. (If the size is wrong, a new array will // be created with the same type.) String[] stringArray = set.toArray(new String[0]); // If you supply an array of the same size as collection or bigger, it // will be populated with collection values and returned (new array // won't be allocated) String[] stringArray2 = set.toArray(new String[set.size()]);
The difference between them is more than just having untyped vs typed results. Their performance can differ as well (for details please read this performance analysis section):
Object[] toArray()
uses vectorizedarraycopy
, which is much faster than the type-checkedarraycopy
used inT[] toArray(T[] a)
.T[] toArray(new T[non-zero-size])
needs to zero the array, whileT[] toArray(new T[0])
does not. Such avoidance makes the latter call faster than the former.
Java SE 8Starting from Java SE 8+, where the concept of
Stream
has been introduced, it is possible to use theStream
produced by the collection in order to create a new Array using theStream.toArray
method.String[] strings = list.stream().toArray(String[]::new);
Examples taken from two answers (1, 2) to Converting 'ArrayList to 'String[]' in Java on Stack Overflow.
-
It is possible to define an array with more than one dimension. Instead of being accessed by providing a single index, a multidimensional array is accessed by specifying an index for each dimension.
The declaration of multidimensional array can be done by adding
[]
for each dimension to a regular array decleration. For instance, to make a 2-dimensionalint
array, add another set of brackets to the declaration, such asint[][]
. This continues for 3-dimensional arrays (int[][][]
) and so forth.
To define a 2-dimensional array with three rows and three columns:
int rows = 3; int columns = 3; int[][] table = new int[rows][columns];
The array can be indexed and assign values to it with this construct. Note that the unassigned values are the default values for the type of an array, in this case
0
forint
.table[0][0] = 0; table[0][1] = 1; table[0][2] = 2;
It is also possible to instantiate a dimension at a time, and even make non-rectangular arrays. These are more commonly referred to as jagged arrays.
int[][] nonRect = new int[4][];
It is important to note that although it is possible to define any dimension of jagged array, it's preceding level must be defined.
// valid String[][] employeeGraph = new String[30][]; // invalid int[][] unshapenMatrix = new int[][10]; // also invalid int[][][] misshapenGrid = new int[100][][10];
How Multidimensional Arrays are represented in Java
Image source: http://math.hws.edu/eck/cs124/javanotes3/c8/s5.html
Jagged array literal intialization
Multidimensional arrays and jagged arrays can also be initialized with a literal expression. The following declares and populates a 2x3
int
array:int[][] table = { {1, 2, 3}, {4, 5, 6} };
Note: Jagged subarrays may also be
null
. For instance, the following code declares and populates a two dimensionalint
array whose first subarray isnull
, second subarray is of zero length, third subarray is of one length and the last subarray is a two length array:int[][] table = { null, {}, {1}, {1,2} };
For multidimensional array it is possible to extract arrays of lower-level dimension by their indices:
int[][][] arr = new int[3][3][3]; int[][] arr1 = arr[0]; // get first 3x3-dimensional array from arr int[] arr2 = arr1[0]; // get first 3-dimensional array from arr1 int[] arr3 = arr[0]; // error: cannot convert from int[][] to int[]
-
The
ArrayIndexOutOfBoundsException
is thrown when a non-existing index of an array is being accessed.Arrays are zero-based indexed, so the index of the first element is
0
and the index of the last element is the array capacity minus1
(i.e.array.length - 1
).Therefore, any request for an array element by the index
i
has to satisfy the condition0 <= i < array.length
, otherwise theArrayIndexOutOfBoundsException
will be thrown.
The following code is a simple example where an
ArrayIndexOutOfBoundsException
is thrown.String[] people = new String[] { "Carol", "Andy" }; // An array will be created: // people[0]: "Carol" // people[1]: "Andy" // Notice: no item on index 2. Trying to access it triggers the exception: System.out.println(people[2]); // throws an ArrayIndexOutOfBoundsException.
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at your.package.path.method(YourClass.java:15)
Note that the illegal index that is being accessed is also included in the exception (
2
in the example); this information could be useful to find the cause of the exception.
To avoid this, simply check that the index is within the limits of the array:
int index = 2; if (index >= 0 && index < people.length) { System.out.println(people[index]); }
-
Object arrays are covariant, which means that just as
Integer
is a subclass ofNumber
,Integer[]
is a subclass ofNumber[]
. This may seem intuitive, but can result in surprising behavior:Integer[] integerArray = {1, 2, 3}; Number[] numberArray = integerArray; // valid Number firstElement = numberArray[0]; // valid numberArray[0] = 4L; // throws ArrayStoreException at runtime
Although
Integer[]
is a subclass ofNumber[]
, it can only holdInteger
s, and trying to assign aLong
element throws a runtime exception.Note that this behavior is unique to arrays, and can be avoided by using a generic
List
instead:List<Integer> integerList = Arrays.asList(1, 2, 3); //List<Number> numberList = integerList; // compile error List<? extends Number> numberList = integerList; Number firstElement = numberList.get(0); //numberList.set(0, 4L); // compile error
It's not necessary for all of the array elements to share the same type, as long as they are a subclass of the array's type:
interface I {} class A implements I {} class B implements I {} class C implements I {} I[] array10 = new I[] { new A(), new B(), new C() }; // Create an array with new // operator and array initializer. I[] array11 = { new A(), new B(), new C() }; // Shortcut syntax with array // initializer. I[] array12 = new I[3]; // { null, null, null } I[] array13 = new A[] { new A(), new A() }; // Works because A implements I. Object[] array14 = new Object[] { "Hello, World!", 3.14159, 42 }; // Create an array with // new operator and array initializer. Object[] array15 = { new A(), 64, "My String" }; // Shortcut syntax // with array initializer.
-
Java SE 8
Converting an array of objects to
Stream
:String[] arr = new String[] {"str1", "str2", "str3"}; Stream<String> stream = Arrays.stream(arr);
Converting an array of primitives to
Stream
usingArrays.stream()
will transform the array to a primitive specialization of Stream:int[] intArr = {1, 2, 3}; IntStream intStream = Arrays.stream(intArr);
You can also limit the
Stream
to a range of elements in the array. The start index is inclusive and the end index is exclusive:int[] values = {1, 2, 3, 4}; IntStream intStream = Arrays.stream(values, 2, 4);
A method similar to
Arrays.stream()
appears in theStream
class:Stream.of()
. The difference is thatStream.of()
uses a varargs parameter, so you can write something like:Stream<Integer> intStream = Stream.of(1, 2, 3); Stream<String> stringStream = Stream.of("1", "2", "3"); Stream<Double> doubleStream = Stream.of(new Double[]{1.0, 2.0});
-
You can iterate over arrays either by using enhanced for loop (aka foreach) or by using array indices:
int[] array = new int[10]; // using indices: read and write for (int i = 0; i < array.length; i++) { array[i] = i; }
Java SE 5// extended for: read only for (int e : array) { System.out.println(e); }
It is worth noting here that there is no direct way to use an Iterator on an Array, but through the Arrays library it can be easily converted to a list to obtain an
Iterable
object.For boxed arrays use Arrays.asList:
Integer[] boxed = {1, 2, 3}; Iterable<Integer> boxedIt = Arrays.asList(boxed); // list-backed iterable Iterator<Integer> fromBoxed1 = boxedIt.iterator();
For primitive arrays (using java 8) use streams (specifically in this example - Arrays.stream -> IntStream):
int[] primitives = {1, 2, 3}; IntStream primitiveStream = Arrays.stream(primitives); // list-backed iterable PrimitiveIterator.OfInt fromPrimitive1 = primitiveStream.iterator();
If you can't use streams (no java 8), you can choose to use google's guava library:
Iterable<Integer> fromPrimitive2 = Ints.asList(primitives);
In two-dimensional arrays or more, both techniques can be used in a slightly more complex fashion.
Example:
int[][] array = new int[10][10]; for (int indexOuter = 0; indexOuter < array.length; indexOuter++) { for (int indexInner = 0; indexInner < array[indexOuter].length; indexInner++) { array[indexOuter][indexInner] = indexOuter + indexInner; } }
Java SE 5for (int[] numbers : array) { for (int value : numbers) { System.out.println(value); } }
It is impossible to set an Array to any non-uniform value without using an index based loop.
Of course you can also use
while
ordo-while
loops when iterating using indices.One note of caution: when using array indices, make sure the index is between
0
andarray.length - 1
(both inclusive). Don't make hard coded assumptions on the array length otherwise you might break your code if the array length changes but your hard coded values don't.Example:
int[] numbers = {1, 2, 3, 4}; public void incrementNumbers() { // DO THIS : for (int i = 0; i < numbers.length; i++) { numbers[i] += 1; //or this: numbers[i] = numbers[i] + 1; or numbers[i]++; } // DON'T DO THIS : for (int i = 0; i < 4; i++) { numbers[i] += 1; } }
It's also best if you don't use fancy calculations to get the index but use the index to iterate and if you need different values calculate those.
Example:
public void fillArrayWithDoubleIndex(int[] array) { // DO THIS : for (int i = 0; i < array.length; i++) { array[i] = i * 2; } // DON'T DO THIS : int doubleLength = array.length * 2; for (int i = 0; i < doubleLength; i += 2) { array[i / 2] = i; } }
Accessing Arrays in reverse order
int[] array = {0, 1, 1, 2, 3, 5, 8, 13}; for (int i = array.length - 1; i >= 0; i--) { System.out.println(array[i]); }
Using temporary Arrays to reduce code repetition
Iterating over a temporary array instead of repeating code can make your code cleaner. It can be used where the same operation is performed on multiple variables.
// we want to print out all of these String name = "Margaret"; int eyeCount = 16; double height = 50.2; int legs = 9; int arms = 5; // copy-paste approach: System.out.println(name); System.out.println(eyeCount); System.out.println(height); System.out.println(legs); System.out.println(arms); // temporary array approach: for(Object attribute : new Object[]{name, eyeCount, height, legs, arms}) System.out.println(attribute); // using only numbers for(double number : new double[]{eyeCount, legs, arms, height}) System.out.println(Math.sqrt(number));
Keep in mind that this code should not be used in performance-critical sections, as an array is created every time the loop is entered, and that primitive variables will be copied into the array and thus cannot be modified.
-
Java SE 5
Since Java 1.5 you can get a
String
representation of the contents of the specified array without iterating over its every element. Just useArrays.toString(Object[])
orArrays.deepToString(Object[])
for multidimentional arrays:int[] arr = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5] int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; System.out.println(Arrays.deepToString(arr)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Arrays.toString()
method usesObject.toString()
method to produceString
values of every item in the array, beside primitive type array, it can be used for all type of arrays. For instance:public class Cat { /* implicitly extends Object */ @Override public String toString() { return "CAT!"; } } Cat[] arr = { new Cat(), new Cat() }; System.out.println(Arrays.toString(arr)); // [CAT!, CAT!]
If no overridden
toString()
exists for the class, then the inheritedtoString()
fromObject
will be used. Usually the output is then not very useful, for example:public class Dog { /* implicitly extends Object */ } Dog[] arr = { new Dog() }; System.out.println(Arrays.toString(arr)); // [Dog@17ed40e0]
-
There are many ways find the location of a value in an array. The following example snippets all assume that the array is is one of the following:
String[] strings = new String[] { "A", "B", "C" }; int[] ints = new int[] { 1, 2, 3, 4 };
In addition, each one sets
index
orindex2
to either the index of required element, or-1
if the element is not present.Using
Arrays.binarySearch
(for sorted arrays only)int index = Arrays.binarySearch(strings, "A"); int index2 = Arrays.binarySearch(ints, 1);
Using a
Arrays.asList
(for non-primitive arrays only)int index = Arrays.asList(strings).indexOf("A"); int index2 = Arrays.asList(ints).indexOf(1); // compilation error
Using a
Stream
Java SE 8int index = IntStream.range(0, strings.length) .filter(i -> "A".equals(strings[i])) .findFirst() .orElse(-1); // If not present, gives us -1. // Similar for an array of primitives
Linear search using a loop
int index = -1; for (int i = 0; i < array.length; i++) { if ("A".equals(array[i])) { index = i; break; } } // Similar for an array of primitives
Linear search using 3rd-party libraries such as org.apache.commons
int index = org.apache.commons.lang3.ArrayUtils.contains(strings, "A"); int index2 = org.apache.commons.lang3.ArrayUtils.contains(ints, 1);
Note: Using a direct linear search is more efficient than wrapping in a list.
Testing if an array contains an element
The examples above can be adapted to test if the array contains an element by simply testing to see if the index computed is greater or equal to zero.
Alternatively, there are also some more concise variations:
boolean isPresent = Arrays.asList(strings).contains("A");
Java SE 8boolean isPresent = Stream<String>.of(strings).anyMatch(x -> "A".equals(x));
boolean isPresent = false; for (String s : strings) { if ("A".equals(s)) { isPresent = true; break; } } boolean isPresent = org.apache.commons.lang3.ArrayUtils.contains(ints, 4);
-
Sorting arrays can be easily done with the Arrays api.
import java.util.Arrays; // creating an array with integers int[] array = {7, 4, 2, 1, 19}; // this is the sorting part just one function ready to be used Arrays.sort(array); // prints [1, 2, 4, 7, 19] System.out.println(Arrays.toString(array));
Sorting String arrays:
String
is not a numeric data, it defines it's own order which is called lexicographic order, also known as alphabetic order. When you sort an array of String usingsort()
method, it sorts array into natural order defined by Comparable interface, as shown below :Increasing Order
String[] names = {"John", "Steve", "Shane", "Adam", "Ben"}; System.out.println("String array before sorting : " + Arrays.toString(names)); Arrays.sort(names); System.out.println("String array after sorting in ascending order : " + Arrays.toString(names));
Output:
String array before sorting : [John, Steve, Shane, Adam, Ben] String array after sorting in ascending order : [Adam, Ben, John, Shane, Steve]
Decreasing Order
Arrays.sort(names, 0, names.length, Collections.reverseOrder()); System.out.println("String array after sorting in descending order : " + Arrays.toString(names));
Output:
String array after sorting in descending order : [Steve, Shane, John, Ben, Adam]
Sorting an Object array
In order to sort an object array, all elements must implement either
Comparable
orComparator
interface to define the order of the sorting.We can use either
sort(Object[])
method to sort an object array on its natural order, but you must ensure that all elements in the array must implementComparable
.Furthermore, they must be mutually comparable as well, for example
e1.compareTo(e2)
must not throw aClassCastException
for any elements e1 and e2 in the array. Alternatively you can sort an Object array on custom order usingsort(T[], Comparator)
method as shown in following example.// How to Sort Object Array in Java using Comparator and Comparable Course[] courses = new Course[4]; courses[0] = new Course(101, "Java", 200); courses[1] = new Course(201, "Ruby", 300); courses[2] = new Course(301, "Python", 400); courses[3] = new Course(401, "Scala", 500); System.out.println("Object array before sorting : " + Arrays.toString(courses)); Arrays.sort(courses); System.out.println("Object array after sorting in natural order : " + Arrays.toString(courses)); Arrays.sort(courses, new Course.PriceComparator()); System.out.println("Object array after sorting by price : " + Arrays.toString(courses)); Arrays.sort(courses, new Course.NameComparator()); System.out.println("Object array after sorting by name : " + Arrays.toString(courses));
Output:
Object array before sorting : [#101 Java@200 , #201 Ruby@300 , #301 Python@400 , #401 Scala@500 ] Object array after sorting in natural order : [#101 Java@200 , #201 Ruby@300 , #301 Python@400 , #401 Scala@500 ] Object array after sorting by price : [#101 Java@200 , #201 Ruby@300 , #301 Python@400 , #401 Scala@500 ] Object array after sorting by name : [#101 Java@200 , #301 Python@400 , #201 Ruby@300 , #401 Scala@500 ]
-
Arrays are objects which provide space to store up to its size of elements of specified type. An array's size can not be modified after the array is created.
int[] arr1 = new int[0]; int[] arr2 = new int[2]; int[] arr3 = new int[]{1, 2, 3, 4}; int[] arr4 = {1, 2, 3, 4, 5, 6, 7}; int len1 = arr1.length; // 0 int len2 = arr2.length; // 2 int len3 = arr3.length; // 4 int len4 = arr4.length; // 7
The
length
field in an array stores the size of an array. It is afinal
field and cannot be modified.This code shows the difference between the
length
of an array and amount of objects an array stores.public static void main(String[] args) { Integer arr[] = new Integer[] {1,2,3,null,5,null,7,null,null,null,11,null,13}; int arrayLength = arr.length; int nonEmptyElementsCount = 0; for (int i=0; i<arrayLength; i++) { Integer arrElt = arr[i]; if (arrElt != null) { nonEmptyElementsCount++; } } System.out.println("Array 'arr' has a length of "+arrayLength+"\n" + "and it contains "+nonEmptyElementsCount+" non-empty values"); }
Result:
Array 'arr' has a length of 13 and it contains 7 non-empty values
-
Java provides several ways to copy an array.
for loop
int[] a = { 4, 1, 3, 2 }; int[] b = new int[a.length]; for (int i = 0; i < a.length; i++) { b[i] = a[i]; }
Note that using this option with an Object array instead of primitive array will fill the copy with reference to the original content instead of copy of it.
Object.clone()
Since Array is considered as an
Object
in Java, you can useObject.clone()
.int[] a = { 4, 1, 3, 2 }; int[] b = a.clone(); // [4, 1, 3, 2]
Note that the
Object.clone
method for an array performs a shallow copy, i.e. it returns a reference to a new array which references the same elements as the source array.
Arrays.copyOf()
java.util.Arrays
provides a easy way to perform the copy of an array to another. Here is the basic usage:int[] a = {4, 1, 3, 2}; int[] b = Arrays.copyOf(a, a.length); // [4, 1, 3, 2]
Note that
Arrays.copyOf
also provides an overload which allows you to change the type of the array:Double[] doubles = { 1.0, 2.0, 3.0 }; Number[] numbers = Arrays.copyOf(doubles, doubles.length, Number[].class);
System.arraycopy()
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.Below an example of use
int[] a = { 4, 1, 3, 2 }; int[] b = new int[a.length]; System.arraycopy(a, 0, b, 0, a.length); // [4, 1, 3, 2]
Arrays.copyOfRange()
Mainly used to copy a part of an Array, you can also use it to copy whole array to another as below:
int[] a = { 4, 1, 3, 2 }; int[] b = Arrays.copyOfRange(a, 0, a.length); // [4, 1, 3, 2]
-
The simple answer is that you cannot do this. Once an array has been created, its size cannot be changed. Instead, an array can only be "resized" by creating a new array with the appropriate size and copying the elements from the existing array to the new one.
String[] listOfCities = new String[3]; // array created with size 3. listOfCities[0] = "New York"; listOfCities[1] = "London"; listOfCities[2] = "Berlin";
Suppose (for example) that a new element needs to be added to the
listOfCities
array defined as above. To do this, you will need to:- create a new array with size 4,
- copy the existing 3 elements of the old array to the new array at offsets 0, 1 and 2, and
- add the new element to the new array at offset 3.
There are various ways to do the above. Prior to Java 6, the most concise way was:
String[] newArray = new String[listOfCities.length + 1]; System.arraycopy(listOfCities, 0, newArray, 0, listOfCities.length); newArray[listOfCities.length] = "Sydney";
From Java 6 onwards, the
Arrays.copyOf
andArrays.copyOfRange
methods can do this more simply:String[] newArray = Arrays.copyOf(listOfCities, listOfCities.length + 1); newArray[listOfCities.length] = "Sydney";
For other ways to copy an array, refer to the following example. Bear in mind that you need an array copy with a different length to the original when resizing.
A better alternatives to array resizing
There two major drawbacks with resizing an array as described above:
- It is inefficient. Making an array bigger (or smaller) involves copying many or all of the existing array elements, and allocating a new array object. The larger the array, the more expensive it gets.
- You need to be able to update any "live" variables that contain references to the old array.
One alternative is to create the array with a large enough size to start with. This is only viable if you can determine that size accurately before allocating the array. If you cannot do that, then the problem of resizing the array arises again.
The other alternative is to use a data structure class provided by the Java SE class library or a third-party library. For example, the Java SE "collections" framework provides a number of implementations of the
List
,Set
andMap
APIs with different runtime properties. TheArrayList
class is closest to performance characteristics of a plain array (e.g. O(N) lookup, O(1) get and set, O(N) random insertion and deletion) while providing more efficient resizing without the reference update problem.(The resize efficiency for
ArrayList
comes from its strategy of doubling the size of the backing array on each resize. For a typical use-case, this means that you only resize occasionally. When you amortize over the lifetime of the list, the resize cost per insert isO(1)
. It may be possible to use the same strategy when resizing a plain array.) -
Java doesn't provide a direct method in
java.util.Arrays
to remove an element from an array. To perform it, you can either copy the original array to a new one without the element to remove or convert your array to another structure allowing the removal.Using ArrayList
You can convert the array to a
java.util.List
, remove the element and convert the list back to an array as follows:String[] array = new String[]{"foo", "bar", "baz"}; List<String> list = new ArrayList<>(Arrays.asList(array)); list.remove("foo"); // Creates a new array with the same size as the list and copies the list // elements to it. array = list.toArray(new String[list.size()]); System.out.println(Arrays.toString(array)); //[bar, baz]
Using System.arraycopy
System.arraycopy()
can be used to make a copy of the original array and remove the element you want. Below an example:int[] array = new int[] { 1, 2, 3, 4 }; // Original array. int[] result = new int[array.length - 1]; // Array which will contain the result. int index = 1; // Remove the value "2". // Copy the elements at the left of the index. System.arraycopy(array, 0, result, 0, index); // Copy the elements at the right of the index. System.arraycopy(array, index + 1, result, index, array.length - index - 1); System.out.println(Arrays.toString(result)); //[1, 3, 4]
Using Apache Commons Lang
To easily remove an element, you can use the Apache Commons Lang library and especially the static method
removeElement()
of the classArrayUtils
. Below an example:int[] array = new int[]{1,2,3,4}; array = ArrayUtils.removeElement(array, 2); //remove first occurrence of 2 System.out.println(Arrays.toString(array)); //[1, 3, 4]
-
Arrays are objects, but their type is defined by the type of the contained objects. Therefore, one cannot just cast
A[]
toT[]
, but each A member of the specificA[]
must be cast to aT
object. Generic example:public static <T, A> T[] castArray(T[] target, A[] array) { for (int i = 0; i < array.length; i++) { target[i] = (T) array[i]; } return target; }
Thus, given an
A[]
array:T[] target = new T[array.Length]; target = castArray(target, array);
Java SE provides the method
Arrays.copyOf(original, newLength, newType)
for this purpose:Double[] doubles = { 1.0, 2.0, 3.0 }; Number[] numbers = Arrays.copyOf(doubles, doubles.length, Number[].class);
-
Array types inherit their
equals()
(andhashCode()
) implementations from java.lang.Object, soequals()
will only return true when comparing against the exact same array object. To compare arrays for equality based on their values, usejava.util.Arrays.equals
, which is overloaded for all array types.int[] a = new int[]{1, 2, 3}; int[] b = new int[]{1, 2, 3}; System.out.println(a.equals(b)); //prints "false" because a and b refer to different objects System.out.println(Arrays.equals(a, b)); //prints "true" because the elements of a and b have the same values
When the element type is a reference type,
Arrays.equals()
callsequals()
on the array elements to determine equality. In particular, if the element type is itself an array type, identity comparison will be used. To compare multidimensional arrays for equality, useArrays.deepEquals()
instead as below:int a[] = { 1, 2, 3 }; int b[] = { 1, 2, 3 }; Object[] aObject = { a }; // aObject contains one element Object[] bObject = { b }; // bObject contains one element System.out.println(Arrays.equals(aObject, bObject)); // false System.out.println(Arrays.deepEquals(aObject, bObject));// true
Because sets and maps use
equals()
andhashCode()
, arrays are generally not useful as set elements or map keys. Either wrap them in a helper class that implementsequals()
andhashCode()
in terms of the array elements, or convert them toList
instances and store the lists. -
Sometimes conversion of primitive types to boxed types is necessary. A common example is
Arrays.asList
which works differently for primitive/boxed arrays:Arrays of boxed types work as expected:
List<Integer> boxed = Arrays.asList(1, 2, 3);
While primitive arrays the return type is a single-element list with the entire array:
List<int[]> primitives = Arrays.asList(new int[]{1, 2, 3});
To convert the array, it's possible to use streams (java 8 and above):
Java SE 8int[] arr = {1,2,3,4}; List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList()); System.out.println(list.toString());
With lower versions it can be more explicitly done by iteration:
Java SE 6int[] arr = {1,2,3,4}; List<Integer> list = new ArrayList<Integer>(arr.length); for (int item : arr) { list.add(item); } System.out.println(list.toString());
The other way round is a little simpler:
Java SE 8Integer[] integerArray = new Integer[]{1, 2, 3, 4, 5}; int[] intArray = new int[integerArray.length]; int len = integerArray.length; for (int i = 0; i < len; ++i) { intArray[i] = integerArray[i]; }
Java SE 8Integer[] integerArray = new Integer[]{1, 2, 3, 4, 5}; int[] intArray = Arrays.stream(integerArray) .mapToInt(Integer::intValue) .toArray(); // The resulting array of ints
Syntax
ArrayType[] myArray;
// Declaring arraysArrayType myArray[];
// Another valid syntax (less commonly used and discouraged)ArrayType[][][] myArray;
// Declaring multi-dimensional jagged arrays (repeat []s)ArrayType myVar = myArray[index];
// Accessing (reading) element at indexmyArray[index] = value;
// Assign value to positionindex
of arrayArrayType[] myArray = new ArrayType[arrayLength];
// Array initialization syntaxint[] ints = {1, 2, 3};
// Array initialization syntax with values provided, length is inferred from the number of provided values: {[value1[, value2]*]}new int[]{4, -5, 6} // Can be used as argument, without a local variable
int[] ints = new int[3]; // same as {0, 0, 0}
int[][] ints = {{1, 2}, {3}, null};
// Multi-dimensional array initialization. int[] extends Object (and so does anyType[]) so null is a valid value.
Parameters
Parameter | Details |
---|---|
ArrayType | Type of the array. This can be primitive (int , long , byte ) or Objects (String , MyObject , etc). |
index | Index refers to the position of a certain Object in an array. |
length | Every array, when being created, needs a set length specified. This is either done when creating an empty array (new int[3] ) or implied when specifying values ({1, 2, 3} ). |
Topic Outline
Introduction
- Creating and Initializing Arrays
- Creating a List from an Array
- Creating an Array from a Collection
- Multidimensional and Jagged Arrays
- ArrayIndexOutOfBoundsException
- Array Covariance
- Arrays to Stream
- Iterating over arrays
- Arrays to a String
- Finding an element in an array
- Sorting arrays
- Getting the Length of an Array
- Copying arrays
- How do you change the size of an array?
- Remove an element from an array
- Casting Arrays
- Comparing arrays for equality
- Converting arrays between primitives and boxed types
Syntax
Parameters
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft