• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 dimensional array

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okey....
My problem goes like this
1. i have method A which gives the size of a 2 dimensional array
2.With the use of the array i need to add elememts resulting from the method B
i declare the array in this way String arr[][] =new String [foundnum][3];
3. within method B i have a
an iteration(for loop)
{
if statement
{
for (i=0; i<foundnum; i++)
{
arr[i][0] ="something";
arr[i][0] ="something";
arr[i][0] ="something";
}
}
if statement2
{
/////my problem starts here
what do i do if i want to continue adding.....
into arr[i][0] ="something";
arr[i][0] ="something";
arr[i][0] ="something";
the i value should start from 2
if i another for loop it overides the previous right?
Can i have the looping out of the if statements?
}

}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm understanding correctly, you want to loop from zero to some number, and then pick up where you left off in another loop. If that's the case, then you basically need your looping index to be declared outside of both loops, so that its scope is not limited to one or the other.

For example, in the following code there are two loops. The first loop starts at zero and goes to some number (0-6) that's decided at runtime. The second loop picks up wherever the first left off. This is possible because both loops use the same variable "index," which is declared to be in scope for both loops.

(Or did I miss the point of your question?)
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Catherine.

I believe it is important that you understand that a multidimensional array is actually an array of arrays:

For instance, the multidimensional array char[][] letters = new char[4][4] will actually look like this:

[0] --> [0] [1] [2] [3]
[1] --> [0] [1] [2] [3]
[2] --> [0] [1] [2] [3]
[3] --> [0] [1] [2] [3]

Where every element in outer array is a reference to another inner array.

Hence, if you want to fill in the first row with letters, you would...



right?

If you want to fill in the second row, then



...and so on, and so on.

As you can see the outer array is just the index of which of the inner arrays you want to interact with.

In other words, the first index represents one of all the arrays in the matrix and the second number, which element of that array.

If you want to visit every cell in all the arrays then you need two nested "for" loops. The outer loop visits every array, the inner loop visits every cell in the current array visited by the outer loop.

Like this:


Now, you can add some logic to this structure and do that you want to do. For instance, you can compare every item to check if it is that your are looking for.

Does this sound helpful?
[ June 12, 2006: Message edited by: Edwin Dalorzo ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic