What is Array
- Array is collection of similar primitive data types, object reference or collection of other array.
- All java array are treated as objects.
- We can construct java array using new keyword.
- All java array elements are initialized by their data type ‘s default value.
How to create Array in JAVA
Three Steps
1. Array Declaration
2. Array Construction
3. Array Initialization
Array Declaration
For declaration array we use empty square brackets with the prefix and postfix of array name.
- Ex.: Declaration for primitive data types
1. Int a[];
2. Int []a;
3. Int[] a;
- Declaration for object reference
Employee emp[];
- Array of array
Int x[][];
Note : 2d array is always array of arrays
Construction of Array:
We can construct array by allocate memory using new keyword at run time
Int a[] ; //Array Declaration
A=new int[5]; // Array construct and able to store 5 int
Note :
Array capacity value can be defined as non zero integer value.
Initilization of Array:
When we construct array . Then all elements of array are initilized by their array data type default value.
For Example :
Data Type | Default Value |
Byte | 0 |
Short | 0 |
Int | 0 |
Long | 0 |
Float | 0.0f |
Double | 0.0d |
Boolean | False |
Char | ‘\u0000’ |
Object | null |
Array Example 1.
public class ArrayExample { public static void main (String[] args) { //Array Declaration int[] a; //Array COnstruction a=new int[5]; //Loop to Read Array for(int i=0;i<a.length;i++) { System.out.println(a[i]); } } }
output
Initilize array Elements
public class ArrayExample { public static void main (String[] args) { //Array Declaration int[] a; //Array COnstruction a=new int[5]; a[0]=10; a[1]=20; a[2]=30; a[3]=40; a[4]=50; //Loop to Read Array for(int i=0;i<a.length;i++) { System.out.println(a[i]); } . }
Output
Another Way to create array
Int[] a={12,34,45,56};
2. 2d Arrays in JAVA
- 2D Arrays is array of array.
- In 2D Arrays first we define memory for row then column.Assigning row and column memory is internal process of java.
- It is similar to2d dynamic array in C/C++;
Rectangular 2d Array
Declaration of 2d array
Int a[][]=new int[3][];
// First and second square brackets defined row and column respectively.
Simple memory initialization for each row
A[0]=new int[2];
A[1]=new int[2];
A[2]=new int[2];
Aa[0][0] | a[a[0][1] |
Aa[1][0] | Aa[1][1] |
Aa[2][0] | Aa[2][1] |
3. Irregular array in Java
Int a[][]=new int[3][];
Simple memory initialization for each row
A[0]=new int[4];
A[1]=new int[2];
A[2]=new int[3];
Note : Now of rows are different.
Example 1.
import java.util.*; public class ArrayExample { public static void main (String[] args) { int a[][]=new int[3][]; a[0]=new int[3]; a[1]=new int[3]; a[2]=new int[3]; for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) { Random rn=new Random(); a[i][j]=rn.nextInt(10); } } for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) { System.out.println("Arrays Element are "+a[i][j]); } } } }
Output
3. 3d array in Java
Declaration of 3d array in Java
Int a[][][]=new int[2][4][3];
- First Square brackets defined number of 2d arrays(here 2)
- Second square bracket defined number of rows in each 2d array.
- Third square bracket defined columns in each rows.
- a.length= no of 2d arrays
- a[i].length=no of rows in the ith 2d array
- A[i][j].length=No of elements in jth row of ith 2d array.
import java.util.*; public class ArrayExample { public static void main (String[] args) { //3d Arrays Example int a[][][]=new int[2][4][3]; for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) { for(int k=0;k<a[i][j].length;k++) { a[i][j][k]=(int)(Math.random()*100); } } } for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++) { for(int k=0;k<a[i][j].length;k++) { System.out.println(a[i][j][k]); } } } } }
Output
Youtube Video 1(Array Introduction)
Youtube Video 2(2d and 3d Array )