ARRAY

DEFINITION

Array is a series of objects all of which are the same size and type. It contains homogeneous data.

DECLARATION OF ARRAY

int arr[] = new int[5];

array

Storage structure of Array in Stack and Heap

If multiple arrays are to be declared ,any syntax of the following can be used:

int ar1[] , ar2[], ar3[];

Or

int[] ar1,ar2,ar3;

PRINTING ELEMENTS OF ARRAY USING  FOR-EACH LOOP


class A{

public static void main(String args[]){
int ar[] = {23,13,12,45,56};
//Using for - each loop
for(int x : ar){
System.out.println(x+"\n");
}
}

ARRAY OF REFERENCE TYPE OF A CLASS

If Student is class :

class Student{
//some attributes
}

And , ar is an Array declared as follows :

Student ar[] = new Student[5];

Then , in memory  it can be represented as :

object-array

2 D ARRAY

An array of arrays can be called as 2D array.

Example :

int ar[][] = new int[3][4];  //This creates an array with 3 rows and 4 columns

Leave a comment