JAVA DAY 5
JAVA DAY 5
8. Array
Advantages
o Code Optimization: It makes the code optimised, we can
retrieve or sort the data efficiently.
o Random access: We can get any data located at an index
position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the
array. It doesn't grow in size at runtime.
o Jagged Array
Syntax
dataType[] a;
dataType []a;
dataType a[];
Instantiation
We can declare, instantiate, and initialise the java array together by:
Syntax
dataType[][] a; (or)
Instantiate
Example
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Example:
class Testarray3
{
public static void main(String args[])
{
//declaring and initialising 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
Output