As we know Array is a datastructure which is a container object that holds a fixed number of values of a single type,such as an array of integers ,characters,objects,etc..Array can be 1-dimensional,2-dimensional(containing rows and columns).
The length of an array is established when the array is created.
The new keyword must be used to allocate memory for an array.
First we will see 1-d Array.
an array in java is declared as:
int a[] or int[] a;
memory is allocated as:
a=new int[10]; - it has a size of 10.
or it can be done as
int a[]=new int[10];
for example :
class anArray
{
public static void main(String p[])
{
int a[]; //array is declared
a=new int[5];
for(int i=0;i<5;i++)
{
a[i]=Integer.parseInt(p[i]); // using command line arguments
}
}
Now we will see 2-d Array,
Declare a 2-dimensional array as follows:
int[][] a; // Declares, but doesn't allocate, 2-dim array.
As with all arrays, the new keyword must be used to allocate memory for an array.
int[][] a= new int[10][5];
This allocates an int array with 10 rows and 5 columns. As with all objects, the values are initialized to zero (unlike local variables which are uninitialized).
for example:
int[][] a = new int[10][5]; // 2-d Array declared
for (int r=0; r
a[r][c]=Integer.parseInt(p[]); // using command line arguments
}
}
Search This Blog
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment