bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Java Array

 

Array is a collection of homogeneous elements that is similar types of elements which are stored in contiguous memory location. In array you can store only fixed set of elements. Array is a index based data structure.

Index of First Element = 0

Index of Last Element = n-1

Where n is the size of array.

 

Types of Array:

There are two types of array:

  • One Dimensional Array
  • Multi Dimensional Array

 

1. One Dimensional Array

oned array

 

Declaration:

Datatype [] Array_name;

Or Datatype Array_name[];

 e.g.

int []i;

or int i[];

 

Instantiation

Array_name=new Daatatype[size of Array];

 e.g.

i= new int[6];

You can do declaration and instantiation together as follows:-

int i[] = new int[6];

 

Example of one dimensional Array

class Intellipaatarray{

public static void main(String args[]){

int i[]=new int[6]; //declaration and instantiation together

i[0]=10;//initialization

i[1]=20;

i[2]=30;

i[3]=40;

i[4]=50;

i[5]=60;

// int i[]={10, 20, 30, 40, 50, 60}; declaration, instantiation and initialization

for(int k=0;i<i.length; k++)

{

System.out.println(i[k]+”\n”);  //print array

}

}

}

Output

10

20

30

40

50

60

 

2. Multi Dimensional Array

 Generally two dimensional array is used in which data is stored in the form of row and column i.e is in matrix form.

Declaration:

Datatype [][]Array_name;

Or Datatype Array_name[][];

e.g.

int [][]i;

or int i[][];

Instantiation

Array_name=new Datatatype[number of rows][number of Columns];

i=new int[2][2];//2 row and 2 column

You can do declaration and instantiation together as follows:-

int i[][] = new int[2][2];

Example of two dimensional Array

class Intellipaatclass{

public static void main(String args[]){

int i[][]={{1,2},{3,4}};  //declaring and initializing 2D array

for(int i=0;i<2;i++){

for(int j=0;j<2;j++){

System.out.print(arr[i][j]+" ");

}

System.out.println(“\n”);

}

}

}

Output

1   2

3   4

 

Passing Array to Method

You can pass the array to the method so that you can use again the similar logic on any array.

Syntax

Method_name(Array_name);

Learn more about Cross-Platform Mobile Technology for Android and iOS using Java in this insightful blog now!

"0 Responses on Java Array"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 04th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Java Array.