Help coderanch get a
new server
by contributing to the fundraiser
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

something more about arrays

 
Greenhorn
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 begening..ie 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++)
{
cout<<"print";
cout<<a[i][j];
}
}

o/p:
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 way...how 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
Swapnil,

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(System.in));

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.

http://www.scit.wlv.ac.uk/~jphb/java/basicio.html
reply
    Bookmark Topic Watch Topic
  • New Topic