Java Array
In this section, you will learn about array in Java.
When you want to store similar kind of data that can be logically group together, your pick can be array. Array is a data structure having fixed size which stores same type of elements in sequential manner. In other words it is a group of variables having common name.
Array is easy to store, easy to iterate. You can retrieve any element of array using its index value. First we will discuss about declaring array in java :
Java array declaration
Array can be broadly classified into two types :
- single dimensional or one dimensional
- multidimensional or more than one dimension
ONE DIMENSIONAL ARRAY
You can declare one dimensional array as follows :
int OneDArray[];
or
int[] OneDArray;
You can instantiate the OneDArray[] as follows :
OneDArray =new int[10];
After the above instantiation, this array can store up to 10 values of type int. The index of the first element will be 0. i.e. ODArray[0] and the last maximum index will be 9 i.e. ODArray[9].
Array initialization and instantiation together can be done as :
int OneDArray[] = {98, 95, 91, 93, 97};
Array iteration
You can iterate the above array as follows :
public class IterateOneDArray { public static void main(String args[]) { int OneDArray[] = {98, 95, 91, 93, 97}; //array iteration using foreach loop for (int element: OneDArray){ System.out.println(element); } } }
Array Sorting
You can sort the above array using sort() method as follows :
import java.util.Arrays; public class OneDArraySort { public static void main(String args[]) { int OneDArray[] = { 98, 95, 91, 93, 97 }; System.out.println("Before sorting: " + Arrays.toString(OneDArray)); Arrays.sort(OneDArray); System.out.println("After sorting: " + Arrays.toString(OneDArray)); } }
The output of the above sorting is :
Before sorting: [98, 95, 91, 93, 97]
After sorting: [91, 93, 95, 97, 98]
Copy Array
You can copy array to another array using copyof() method as follows
import java.util.Arrays; public class ArrayCopy { public static void main(String args[]) { String arr[] = { "a", "b", "c" }; String copyArr[] = Arrays.copyOf(arr, 3); System.out.printf("Value of arr \t\t%s\n", Arrays.toString(arr)); System.out.printf("Value of copyArr\t\t%s", Arrays.toString(copyArr)); } }
The output of the above code is given below :
Value of arr [a, b, c]
Value of copyArr [a, b, c]
MULTI-DIMENSIONAL ARRAY
Multidimensional (or more than one dimension) arrays are arrays of arrays. A two-dimensional array can be declared as :
int twoDArray[][];
or
int[][] twoDArray;
You can instantiate the twoDArray[] as follows :
int twoDArray[][] = new int[4][5];
Means this two array have 4 rows and 5 columns.
The above array initialization and instantiation together can be done as :
int twoDArray[][] = { { 2, 1, 3 }, { 1, 3, 2 }, { 3, 2, 1 } };
You can iterate the above array as follows :
public class IterateTwoDArray { public static void main(String args[]) { int twoDArray[][] = { { 2, 1, 3 }, { 1, 3, 2 }, { 3, 2, 1 } }; for (int row = 0; row < twoDArray.length; row++) { for (int col = 0; col < twoDArray[row].length; col++) { int element= twoDArray[row][col]; System.out.print(element+" "); } System.out.println(); } } }
The output of the above code will be :
2 1 3
1 2 2
3 2 1