Arrays In Java
These are collection of similar types of data types having a contiguous (adjacent) memory location.
Advantages of arrays:
1. It increases the performances of program.
2. It reduces the size of a program.
In Java, arrays are also represented via objects. Once an array got a memory then it cannot be changed (increased or decreased). But JAVA made these arrays as dynamic.
Different ways to define and initialize an array:
We can an array at class level or locally(inside a function).
1) Defining an array at class level:
classTemp
{
//array defined at class level
intx[]=newint[size];
publicstaticvoidmain(String[]args)
{
}
}
2) Defining an array inside a function(locally):
classTemp
{
publicstaticvoidmain(String[]args)
{
//array defined inside a function
intx[]=newint[size];
}
}
3) Second way to define an array:
classTemp
{
//we defined an array of 5 elements(values assigned)
intx[]={10,20,30,40,50};
publicstaticvoidmain(String[]args)
{
}
}
In this, we initialized an array with the values and the number of elements inside the elements block is the size of the array.
4) Third way to define an array (via anonymous array):
we can also use anonymous arrays. anonymous arrays do not have any name and they cannot be reused.
classTemp
{
//anonymous array initialization
intx[]=newint[]{1,2,4};
publicstaticvoidmain(String[]args)
{
}
}
Array of arrays (multi dimensional arrays)
arrays of arrays are the arrays joined together contiguously. These are also referred as multidimensional array.
Syntax and representation of multidimensional array:
int x[] [];
In this the number in first array box represents the number of arrays and in the second array box, it represent the size of each array.
For Example if we write array as int[3][3] , it means three arrays of size 3 each are joined together to form a two dimensional array.
Initialization of array of arrays:
intx[][]={
{10,20,30,40},
{50,60}’
{70,80,90},
};
or we can also use anonymous arrays to initialize a 2D array:
newint[][]{
{10,20,30};
{40,50};
{60};
};
Some basic programs of array usage:
Example: Defining a simple array of size 5 and initializing the values at class level.
classTemp
{
intmyArray[]=newint[]{1,2,3,4,5};
publicstaticvoidmain(String[]args)
{
//as array is non static so we have to create
//an object to call it in static main()
Tempt=newTemp();
//array.length will give the length of this array
for(inti=0;i<myArray.length;i++)
{
System.out.println(t.myArray[i]);
}
}}
/* Output
1
2
3
4
5
*/
Example: passing array as a argument in a method
classTemp
{
//the method show will accept int type of array
staticvoidshow(int[]array)
{
for(inti=0;i<array.length;i++)
{
System.out.println(array[i]);
}
}
publicstaticvoidmain(String[]args)
{
intmyArray[]=newint[]{1,2,3,4,5};
show(myArray);//passed the array to the show() method
}}
/*Output
1
2
3
4
5
*/
Example: Using anonymous array
classTemp
{
//the method show will accept int type of array
staticvoidshow(int[]array)
{
for(inti=0;i<array.length;i++)
{
System.out.println(array[i]);
}
}
publicstaticvoidmain(String[]args)
{
//passed the anonymous array to the show() method
show(newint[]{1,2,3,4,5});
}}
/*Output
1
2
3
4
5
*/
Example: Similarly, we can use multidimensional array, but in case of 2D array, we have to use two for loop.
In the first loop, the X[0], x[1], x[2] parts will be accessed and in the inner(second) for loop we would be able to access x[0][0], x[0][1], x[0][2], x[1][0] and so on…
classTemp
{
//multi array defined with name "x"
intx[][]=newint[][]{
{2,4,6},
{3,6,9},
{4,8,12}
};
publicstaticvoidmain(String[]args)
{
Tempt=newTemp();
for(inti=0;i<3;i++)
{
for(intj=0;j<3;j++)
{
System.out.println(t.x[i][j]);
}}
}}
Output:
No comments:
Post a Comment