Java Arrays
Java Arrays
datatype [] arrayName;
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
As we can see, each element of the
multidimensional array is an array itself. And
also, unlike C/C++, each row of the
multidimensional array in Java can be of
different lengths.
Initialization of
2-dimensional Array
Example: 2-dimensional Array
class MultidimensionalArray {
public static void
main(String[] args) {
// create a 2d array
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
// calculate the length of
each row
System.out.println("Length
of row 1: " + a[0].length);
System.out.println("Length
of row 2: " + a[1].length);
System.out.println("Length
of row 3: " + a[2].length);
}
}
Run Code
Output:
Length of row 1: 3
Length of row 2: 4
Length of row 3: 1
In the above example, we are creating a
multidimensional array named a. Since each
component of a multidimensional array is also
an array (a[0], a[1] and a[2] are also
arrays).
int[][] a = {
{1, -2, 3},
{-4, -5, 6, 9},
{7},
};
for (int i = 0; i < a.length; +
+i) {
for(int j = 0; j < a[i].length; +
+j) {
System.out.println(a[i][j]);
}
}
}
}
Run Code
Output:
1
-2
3
-4
-5
6
9
7
We can also use the for...each loop to access
elements of the multidimensional array. For
example,
class MultidimensionalArray {
public static void
main(String[] args) {
// create a 2d array
int[][] a = {
{1, -2, 3},
{-4, -5, 6, 9},
{7},
};
// first for...each loop
access the individual array
// inside the 2d array
for (int[] innerArray: a)
{
// second for...each
loop access each element inside
the row
for(int data:
innerArray) {
System.out.println(data);
}
}
}
}
Run Code
Output:
1
-2
3
-4
-5
6
9
7
// test is a 3d array
int[][][] test = {
{
{1, -2, 3},
{2, 3, 4}
},
{
{-4, -5, 6, 9},
{1},
{2, 3}
}
};
Basically, a 3d array is an array of 2d arrays.
The rows of a 3d array can also vary in length
just like in a 2d array.
Example: 3-dimensional Array
class ThreeArray {
public static void
main(String[] args) {
// create a 3d array
int[][][] test = {
{
{1, -2, 3},
{2, 3, 4}
},
{
{-4, -5, 6, 9},
{1},
{2, 3}
}
};
// for..each loop to
iterate through elements of 3d
array
for (int[][] array2D:
test) {
for (int[] array1D:
array2D) {
for(int item:
array1D) {
System.out.println(item);
}
}
}
}
}
Run Code
Output:
1
-2
3
2
3
4
-4
-5
6
9
1
2
3
class Main {
public static void
main(String[] args) {
int [] source = {1, 2, 3,
4, 5, 6};
int [] destination = new
int[6];
System.out.println(Arrays.toString
(destination));
Here, the toString() method is used to convert
an array into a string. To learn more, visit
the toString() method (official Java
documentation).
class Main {
public static void
main(String[] args) {
int[] n1 = {2, 3, 12, 4,
12, -2};
int[] n3 = new int[5];
// Creating n2 array of
having length of n1 array
int[] n2 = new
int[n1.length];
// copying entire n1 array
to n2
System.arraycopy(n1, 0,
n2, 0, n1.length);
System.out.println("n2 = "
+ Arrays.toString(n2));
// copying elements from
index 2 on n1 array
// copying element to
index 1 of n3 array
// 2 elements will be
copied
System.arraycopy(n1, 2,
n3, 1, 2);
System.out.println("n3 = "
+ Arrays.toString(n3));
}
}
Run Code
Output:
n2 = [2, 3, 12, 4, 12, -2]
n3 = [0, 12, 4, 0, 0]
In the above example, we have used
the arraycopy() method,
System.arraycopy(n1, 0, n2, 0,
n1.length) - entire elements from
the n1 array are copied to n2 array
System.arraycopy(n1, 2, n3, 1,
2) - 2 elements of the n1 array starting from
index 2 are copied to the index starting
from 1 of the n3 array
As you can see, the default initial value of
elements of an int type array is 0.
4. Copying Arrays Using copyOfRange()
method
We can also use the copyOfRange() method
defined in Java Arrays class to copy arrays.
For example,
// To use toString() and
copyOfRange() method
import java.util.Arrays;
class ArraysCopy {
public static void
main(String[] args) {
int[] source = {2, 3, 12,
4, 12, -2};
// copying entire source
array to destination
int[] destination1 =
Arrays.copyOfRange(source, 0,
source.length);
System.out.println("destination1 =
" +
Arrays.toString(destination1));
// copying from index 2 to
5 (5 is not included)
int[] destination2 =
Arrays.copyOfRange(source, 2, 5);
System.out.println("destination2 =
" +
Arrays.toString(destination2));
}
}
Run Code
Output
destination1 = [2, 3, 12, 4, 12, -
2]
destination2 = [12, 4, 12]
In the above example, notice the line,
int[] destination1 =
Arrays.copyOfRange(source, 0,
source.length);
Here, we can see that we are creating
the destination1 array and copying
the source array to it at the same time. We
are not creating the destination1 array
before calling the copyOfRange() method.
To learn more about the method, visit Java
copyOfRange.
class Main {
public static void
main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
class Main {
public static void
main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
int[][] destination = new
int[source.length][];
// allocating space
for each row of destination array
destination[i] = new
int[source[i].length];
System.arraycopy(source[i], 0,
destination[i], 0,
destination[i].length);
}
// displaying destination
array
System.out.println(Arrays.deepToSt
ring(destination));
}
}
Run Code
Output:
[[1, 2, 3, 4], [5, 6], [0, 2, 42,
-4, 5]]
Here, we can see that we get the same output
by replacing the inner for loop with
the arraycopy() method.
1 array[i] = value;
6
1
7 }
1
8
1 // getting array length
9 int length = array.length;
2
0
// default sium value.
2
1 int sum = 0;
2
2
// sum of all values in array
2 using for loop
3
for (int i = 0; i < array.length;
2
4 i++) {
2 sum += array[i];
5
}
2
6
2 double average = sum /
7
2 length;
8
2
9 System.out.println("Average of
array : " + average);
3
0
3 }
1
3
2 }
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
Output:
1 Enter array size:
2 5
3
Enter array values :
4
12
5
6 23
7 34
8 45
9
56
Average of array : 34.0
• Reverse an array
Program:
public class SortAsc {
public static void main(String[] args)
{
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println();
// Main class
class GFG {
1 public static String toString(char[] a)
{
// Creating object of String class
String string = new String(a);
return string;
}
public static void main(String args[])
{
import java.util.Arrays;
System.arraycopy(array1,
0, result, 0, aLen);
System.arraycopy(array2,
0, result, aLen, bLen);
System.out.println(Arrays.toString
(result));
}
}
Output
[1, 2, 3, 4, 5, 6]
In the above program, we've two integer
arrays array1 and array2.
In order to combine (concatenate) two arrays,
we find its length stored
in aLen and bLen respectively. Then, we
create a new integer array result with
length aLen + bLen.
Now, in order to combine both, we copy each
element in both arrays to result by
using arraycopy() function.
The arraycopy(array1, 0, result,
0, aLen) function, in simple terms, tells the
program to copy array1 starting from
index 0 to result from index 0 to aLen.
Likewise, for arraycopy(array2, 0,
result, aLen, bLen) tells the program to
copy array2 starting from
index 0 to result from index aLen to bLen.
Example 2: Concatenate Two Arrays without
using arraycopy
import java.util.Arrays;
System.out.println(Arrays.toString
(result));
}
}
Output
[1, 2, 3, 4, 5, 6]
In the above program, instead of
using arraycopy, we manually copy each
element of both
arrays array1 and array2 to result.
We store the total length required
for result, i.e. array1.length +
array2. length. Then, we create a new
array result of the length.
Now, we use the for-each loop to iterate
through each element of array1 and store it
in the result. After assigning it, we increase
the position pos by 1, pos++.
Likewise, we do the same for array2 and
store each element in result starting from
the position after array1.