This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes something more about arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "something more about arrays" Watch "something more about arrays" New topic

something more about arrays

swapnil deo

Joined: May 30, 2007
Posts: 6
thanks everyone for your replies...but still im not clear with this concept of "array of arrays"

Campbell Ritchie : i went through the link which you gave and read things about array but the program over there was as follows : class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declares an array of integers

anArray = new int[10]; // allocates memory for 10 integers

anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;

System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);

The output from this program is:

Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000

im pretty clear with the above code
now in the above code we are initializing the array at the very in the code itself..but my doubt was how do we insert values in runtime like in the following C++ code :
int a[][];
int x;
cout<< "enter dim of array";
cin>> x;
cout << "enter array elements";

for(int i=0;i<x;i++)//this for loop will ask user to enter values at runtime
for (int j=0;j<x;j++)
cin>>a[i][j]; //user will enter values when this line gets executed
for(int i=0;i<x;i++)//this for loop will print the output
for (int j=0;j<x;j++)

enter dim of array:
3 3
enter array elements:
1 2 3 4 5 6

print: 1 2 3 4 5 6

now in this do i get to enter values to 2-dimensional arrays in java..???
this is my real doubt...???
thanks for your time..!!
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148

In Java, there is no >> operator (or handy cin object) like in C++. Instead you need to create a BufferedReader object to read user input for you, like this:

BufferedReader reader = new BufferedReader(new InputStreamReader(;

Then you use the assignment operator inside your loop like this:

a[i][j] = reader.readLine();

Of course, just as in C++, this operation is not type-safe. You need to make sure you convert the input to whatever type the array a[][] is. (Or what would happen when the user types in his name instead of a number?)

For a full example of how to read user input (complete with exception handling) see the following link.
I agree. Here's the link:
subject: something more about arrays
It's not a secret anymore!