Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
  • Piet Souris

something more about arrays

Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!!
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Low Tech Laboratory
    Bookmark Topic Watch Topic
  • New Topic