Arrays in Java Programming Language

Posted on

 

Arrays

         An array can be defined as a collection of similar elements. Java supports three types of arrays namely

  • Single dimensional.
  • Double dimensional.
  • Multi dimensional.

A single dimensional array can be created in the following format.

Syntax:data_type array_name[] = new data_type[size];

(or)

data_type array_name[];

array_name = new data_type[size];

Example:
int x[] = new int[5]; (or) int x[];
x = new int[5];

 

class App10{

public static void main(String yck[])
{

int x[]=new int [5];
int i,k;
for(i=0,k=10;i<4;i++,k+=10)
x[i]=k;
System.out.println(“The elements in the array are ………”);
for(i=0;i<4;i++)      //for(i=0;i<x.length;i++)
System.out.println(x[i]+” “);
}

}

 

output:
\>javac App10.java
\>java App10
The elements in the array are ………
10
20
30
40
Length property: Whenever you create an array in Java, automatically a special property called length is attached to it. The length property returns the size of the array.

 

Example: for(i=0; i<x.length; i++)

A double dimensional array is a matrix of rows and columns. A two dimensional array can be declared in the following format…

Syntax:data_type array_name[][]=new data_type array_name[rows][columns];

(or)

data_type array_name[][];

array_name = new data_type array_name[rows][columns];

 

class App11{

public static void main(String yck[])
{

int x[][]=new int [3][3];
int r,c;
for(r=0;r<=2;r++)
{

for(c=0;c<=2;c++)
{

if(r==c)
x[r][c]=1;
else
x[r][c]=0;
}
}
System.out.println(“The elements in the Matrix are ………”);
for(r=0;r<=2;r++)        //for(r=0;r<x.length;r++)
{

for(c=0;c<=2;c++)      //for(c=0;c<x[r].length;c++) <dd=””> System.out.print(x[r][c]+” “);</x[r].length;c++)>
System.out.println();
}
}

}

 

output:
\>javac App11.java
\>java App11
The elements in the Matrix are ………
1 0 0
0 1 0
0 0 1

 

class App12{

public static void main(String yck[])
{

int x[][]=new int [3][3];
int r,c,k;
for(r=0,k=1;r<=2;r++)
{

for(c=0;c<=2;c++,k++)
x[r][c]=k;
}
System.out.println(“The elements in the Matrix are ………”);
for(r=0;r<=2;r++)       //for(r=0;r<x.length;r++)
{

for(c=0;c<=2;c++)       //for(c=0;c<x[r].length;c++)
System.out.print(x[r][c]+” “);
System.out.println();
}
}

}

 

output:
\>javac App12.java
\>java App12
The elements in the Matrix are ………
1 2 3
4 5 6
7 8 9

A multidimensional array can be declared in the following format…..

Syntax:data_type array_name[][][]=new data_type array_name[x][y][z];

or

data_type array_name[][][];

array_name = new data_type array_name[x][y][z];

Note: Java allows you to create a variety of arrays with a fixed no. of rows and each row varies with different no. of cell’s.

Example: App13.java

 

class App13{

public static void main(String yck[])
{

int x[][]=new int [3][];
x[0]=new int[5];
x[1]=new int[2];
x[2]=new int[8];
int r,c,k;
for(r=0,k=1;r<=2;r++)
{

for(c=0;c<x[r].length;c++,k++) <dd=””> x[r][c]=k;</x[r].length;c++,k++)>
}
System.out.println(“The elements in the Matrix are ………”);
for(r=0;r<=2;r++)       //for(r=0;r<x.length;r++)
{

for(c=0;c<x[r].length;c++) <dd=””> System.out.print(x[r][c]+” “);</x[r].length;c++)>
System.out.println();
}
}

}

 

output:
\>javac App13.java
\>java App13
The elements in the Matrix are ………
1 2 3 4 5
6 7
8 9 10 11 12 13 14 15

Leave a Reply

Your email address will not be published. Required fields are marked *