Credits

Saturday, February 28, 2009

Single Dimensional Array in Java

Just like in C or C++, Java possesses advanced data types like array. We define array as a collection of similar elements.

A general syntax in Java for array elements is the one below:

data_type arrayname[ ] = new data_type[size];
eg.
int sample[]=new int[10];

other form of syntax declarations could be this:
int sample[]={1,2,3,4}; /* the number of initial elements tell the size of the array*/



/* sample one dimensional array elements*/

//program to assign array elements
class onedarray{
public static void main (String [] args){
int sample[]=new int[10];
int i;
for(i=0;i<10;i++)
sample[i]=i;
for(i=0;i<10;i++)
System.out.println("Sample ["+i+"]:"+sample[i]);
}
}

What is the output of this code?

AddThis