Arrays in Java

In Java array is a collection of elements of same type. The array cannot be resized dynamically, for this use any list class, for example ArrayList

Square brackets [] are used to declare an array and as an element access operator.

The Arrays class contains some useful utility methods to handle arrays. For fast copying one array to another you can use the System.arraycopy() method.

 // array declaration
String a [];
String [] a1;

// array declaration with initialization
String a2 [] = new String[2]; // empty array, every element is null 
String a3 [] = new String[] {"str1", null, "str2"}; // with elements


//  array with elements of primitive type
int nums [] = new int [] {1,2,3};
int nums1 [] = new int[3]; // every element zero 

// access to element, elements are indexed from 0
String s = a3[0];
String s1 = a3[1];

// change value
a3[2] = "some new value";

// assign new array to the variable
a2 = a3;

iterate over array

// iterate over array
for(String el: a3){
    System.out.println(el);
}
    
// iterate over array by index   
for(int i=0; i<a3.length; ++i){
     System.out.println(a3[i]);
}

// iterate via stream
Arrays.stream(a3).forEach(el -> System.out.println(el));

compare arrays

if(Arrays.equals(intArray1,intArray2)){
    // ...
}

// deep compare
if(Arrays.deepEquals(objArray1,objArray2)){
    // ...
}

array/list conversion

String[] strArray = { "a", "b", "c", "d", "e" };
        
// array to list
List<String> strList = Arrays.asList(strArray);
        
// list to array
String [] strArray2 = new String[strList.size()];
strList.toArray(strArray2);

Arrays class

method description
static methods
asList(T... a) Returns a fixed-size list backed by the specified array.
binarySearch(a, key) Searches the specified array for the specified value using the binary search algorithm.
binarySearch(a, from, to, key) Searches a range of the specified array for the specified value using the binary search algorithm.
binarySearch(aObj, from, to, key, cmp) Searches a range of the specified array for the specified object using the binary search algorithm and given comparator.
binarySearch(aObj, key, cmp) Searches the specified array for the specified object using the binary search algorithm and given comparator.
copyOf(src, newLen) Copies the specified array, truncating or padding with default value (depending on the type) so the copy has the specified length.
copyOfRange(src, from, to) Copies the specified range of the specified array into a new array.
deepEquals(a1, a2) Returns true if the two specified arrays are deeply (per elements) equal to one another.
deepHashCode(a) Returns a hash code based on the "deep contents" of the specified array of objects.
deepToString(a) Returns a string representation of the "deep contents" of the specified array.
equals(a, a2) Returns true if the two specified arrays are equal to one another.
fill(a, val) Fills an array with the specified value.
fill(a, from, to, val) Fills a range of an array with the specified value.
hashCode(a) Returns a hash code based on the contents of the specified array of elements of primitive types.
sort(a) Sorts the specified array into ascending order.
sort(a, from, to) Sorts the specified range of the specified array into ascending order.
sort(a, cmp) Sorts the specified array of objects according to the order induced by the specified comparator.
sort(a, from, to, cmp) Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
toString(a) Returns a string representation of the array content.