You are on page 1of 2

Array :

------
Array is a collection of similar type of elements which has continuous memory
locations.

Array is index-based, the first element of the array is stored at the 0th index, 1,
2nd element is stored on 1st index and so on.

Last element position in Array is Array size-1.

We can store only a fixed set of elements in a Java array.

Types of Arrays : There are three types of arrays.


---------------
1 Single Dimensional Array

2 Multidimensional Array

3 Jagged Array

Single Dimensional Array:


-------------------------

Declaration of Array
--------------------

Eg:int a[]; or int []a; or int[] a;

Instantiation of an Array
-------------------------
int a[];

a=new int[3];0 1 2

Initialisation of an Array
--------------------------
int a[];---->Declaration

a=new int[3];--->Instantiation

a[0]=11;---->Initialisation
a[1]=12;
a[2]=13;

Declaration, Instantiation and Initialization of Array in single line


---------------------------------------------------------------------
int a[]={33,3,4,5}; or int a[]=new int[]{10,20,30};
For-each Loop:
-------------
We can also print the array using for-each loop. for-each loop prints the array
elements one by one.

It holds an array element in a variable, then executes the body of the loop.

Ex:
---
int a[]=new int[10];

for(int i:a){
body
}

Multidimensional Array : An Array having more than one dimension.


------------------------

Ex:
---
int[][] arr=new int[3][3];

column
0 1 2
Row--->0 1 2 3
1 4 5 6
2 7 8 9

0,0=1 0,1=2 0,2=3


1,0=4 1,1=5 1,2=6

Jagged Array:
-------------
Main intention of jagged array is to declar customized array.

The elements of a jagged array can be of different dimensions and sizes.

EX:
---
int[] arr=new int[3][3];

arr[0]=new int[5];
arr[1]=new int[4];
arr[2]=new int[2];

You might also like