Sunday, 4 December 2016

Java - Array Explanation

Array:-
        Array is a collection of items with same data type.
        Array elements are stored in consecutive memory location.
Syntax to 1D array:-
datatype arrayname[]=new datatype[arraysize];
eg:-
1) int a[]=new int[5];
2) int []a=new int[5];
3) int a[];//declaration
          a=new int[5];//memory allocation
4) int a[]={1,2,3,4,5};
5) int a[]=new int[]{1,2,3,4,5};

Syntax to 2D array:-
datatype arrayname[][]=new datatype[rowsize][colsize];
eg:-
1) int a[][]=new int[2][5];
2) int [][]a=new int[5][5];
3) int a[][];
        a=new int[5][5];
4) int a[][]={{1,2},{3,4},{5,6}};

5) int a[][]=new int[][]{{1,2,7,9},{3,4},{5,6}};

No comments:

Post a Comment

Featured post

Development Of JAVA Program