Array In Java
Array
In Java, array is a collection of similar types of data, which have adjacent memory location. Java array is a data structure, where we store similar types of elements. We can store only fixed set of elements in a Java array.
Array in Java is index based and first element of the array is stored at 0 index.
In other words, we can say that an array is a container object, which holds the values of homogenous type. It is also called as a static data structure. The size of an array is specified at the time of an array declaration.
An array can be either primitive type or reference type and it gets memory in heap area.
Types of Array in Java
In Java, two types of array are there, which are,
- Single dimensional array
Syntax
dataType[] arr; or
dataType []arr; or
dataType arr[];
Instantiation
arrayRefVar=new datatype[size];
Let’s see an example.
Code- public class JavaArray {
- public static void main(String args[]) {
- int arr[] = new int[10]; //declaration and instantiation of an array
- arr[0] = 25; //array initialization
- arr[1] = 30;
- arr[2] = 35;
- arr[3] = 40;
- arr[4] = 45;
- arr[5] = 50;
- arr[6] = 55;
- arr[7] = 60;
- arr[8] = 65;
- arr[9] = 70;
- for (int i = 0; i < arr.length; i++) //length is the property of array
- {
- System.out.println(arr[i]);
- }
- }
- }
Output
In the example, mentioned above, first we declare, instantiate and initialize of an array. In Java, we can declare, instantiate and initialize the array simultaneously.
Let’s see an example, given below.
Code- public class JavaArray {
- public static void main(String args[]) {
- int arr[] = {10, 45, 22, 5};//declaration, instantiation and initialization
- for (int i = 0; i < arr.length; i++) {
- System.out.println(arr[i]);
- }
- }
- }
Output
In the example, mentioned above, we declare, instantiate and initialize the array simultaneously. Afterwards, traverse all elements of an array. - Multidimentional array
In Java, when data or elements are stored in row and column, based on index, which is called as a a multidimentional array.
Syntax
dataType[][] arrayRefVar; or
dataType [][]arrayRefVar; or
dataType arrayRefVar[][]; or
dataType []arrayRefVar[];
Instantiation
int[][] arr=new int[3][3];//3 row and 3 column
Let’s see an example, given below.
Code- public class JavaArray {
- public static void main(String args[]) {
- //declaring and initializing 2D array
- int arr[][] = {{3, 5, 7}, {7, 8, 5}, {6, 4, 7}};
- for (int a = 0; a < 3; a++) {
- for (int b = 0; b < 3; b++) {
- System.out.print(arr[a][b] + " ");
- }
- System.out.println();
- }
- }
- }
Output
In the example, shown above, first we declare, instantiate, initialize and print the multidimentional array output.
Passing Array
In Java, we can pass an array to the method. Therefore, we can reuse the same logic on any array.
Let’s see an example, given below.
Code
- public class JavaArray {
- static void minElement(int arr[]) {
- int minElement = arr[0];
- for (int i = 1; i < arr.length; i++) {
- if (minElement > arr[i]) {
- minElement = arr[i];
- }
- }
- System.out.println(minElement);
- }
- public static void main(String args[]) {
- int a[] = {15, 39, 40, 56};
- minElement(a);//passing array to method
- }
- }
Output
In the example, mentioned above, we get minimum number of an array passing to method.
Class name of Java array
Array is an object in Java. For array object, a proxy class is created, whose name can be obtained with the help of getClass().getName() method on the object.
Let’s see an example, given below.
Code
Code
- public class JavaArray {
- public static void main(String args[]) {
- int arr[] = {3, 7, 5};
- Class c1 = arr.getClass();
- String name = c1.getName();
- System.out.println(name);
- }
- }
Output
Copy an Array
We can copy an array to another with the use of arraycopy() method of System class.
Syntax
public static void arraycopy(
Object src, int srcPos,Object dest, int destPos, int length
)
Let’s see an example, given below.
Code
- public class JavaArray {
- public static void main(String[] args) {
- char[] copyFrom = {'j', 'a', 'v', 'a', 'i', 's', 'a', 'c', 'o', 'f', 'f', 'e', 'e'};
- char[] copyTo = new char[6];
- System.arraycopy(copyFrom, 7, copyTo, 0, 6);
- System.out.println(new String(copyTo));
- }
- }
Output
Addition of two matrices
Let’s see an example, given below.
Code
- public class JavaArray {
- public static void main(String args[]) {
- //creating two matrices
- int x[][] = {{1, 6, 4}, {7, 4, 5}};
- int y[][] = {{2, 9, 4}, {3, 8, 4}};
- //matrix to store the sum of two matrices
- int z[][] = new int[2][3];
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 3; j++) {
- z[i][j] = x[i][j] + y[i][j];
- System.out.print(z[i][j] + " ");
- }
- System.out.println();//new line
- }
- }
- }
Output
In the example, mentioned above, add two matrices.
Advantages of Java Array
- Code Optimization
- Random access
Disadvantage of Java Array
- Size Limit
Summary
Thus, we learnt that Java array is a data structure, where we store similar types of the elements and also learn their types, passing an array and copy an array in Java.