Yes, easily, assuming it's a true matrix (i.e., every row is the same length). Here's how, with one loop, and no other control structures like "if":
String array[][] = new String[][] {
{ "A(0,0)", "A(0,1)", "A(0,2)" },
{ "A(1,0)", "A(1,1)", "A(1,2)" },
{ "A(2,0)", "A(2,1)", "A(2,2)" } };
int rowLen = array[0].length;
int max = array.length * rowLen;
for (int i=0; i
System.out.println("i=" + i + ": " +
array[i / rowLen][i % rowLen]);
Note that I didn't bother coding for a zero-size array. But this should give you an idea.
When I execute this code, it prints out:
i=0: A(0,0)
i=1: A(0,1)
i=2: A(0,2)
i=3: A(1,0)
i=4: A(1,1)
i=5: A(1,2)
i=6: A(2,0)
i=7: A(2,1)
i=8: A(2,2)
=====================
It can be done if the rows are different lengths, but it's more complicated and requires some "if" statements. You'd step through the elements of each row, moving to the next row when you hit the end of one. Something like this:
boolean done=false
int rowIdx = 0; colIdx = 0;
while (! done) {
System.out.println( array[rowIdx][colIdx]);
colIdx++;
if (colIdx >= array[rowIdx].length) {
colIdx = 0;
rowIdx++;
if (rowIdx >= array.length) done = true;
}
}
2007-08-04 05:16:59
·
answer #1
·
answered by McFate 7
·
0⤊
1⤋
All you need to do is use a loop variable that spans that spans the entire length of the array and use modulo and int division to determine the row and colomn indices. Ex arr[5][7] has a length of 5*7 = 35 for ( int k=0; k<35; k++ ) { ...arr[k/5][k%7] // gives you the correct row and column element. } check; when k = 8 this should be the row 1, col 1 (indexing from 0, of courst) (int) 8/5 = 1 8%7 = 1 ta da +add That wasn't a good check because the row and column indices are both 0. Let's get the location of that last element. That element occurs when k = 34 row: (int) 34/5 = 6 col: 34%7 = 6 also a bad example! Let's do it backwards: array[3][2] should occur at 3*row dim + 2rd column element or k = 15 + 2 -1 = 16 (-1 because indexing starts at 0) (int) 16/5 = 3 16%7 = 2 That's the 17th element of the array.
2016-05-17 23:36:56
·
answer #2
·
answered by ? 3
·
0⤊
0⤋
String[][] myArray =
{ { "A1", "A2", "A3" },
{ "B1", "B2", "B3" },
{ "C1", "C2", "C3" },
};
void print( Object object )
{ Class tempClass = object.getClass().getComponentType();
if (tempClass == null)
{ System.out.println(object.toString());
return;
}
Object[] objectArray = (Object[])object;
for (int i = 0; i < objectArray.length; i++)
{ print(objectArray[i]);
}
}
. . . print(myArray); . . .
this will handle any number of dimensions, but formatting beyond two gets strange. I assume you can handle the formatting? Anyway, nested loops are easier to read and troubleshoot.
p.s. Afterthought: I hope this is not for a school assignment.
***edited:
Seeing a "Top Contributor" answer "no", reminded me of a computer department manager that insisted that the language they used did not allow conditional statements :P
Just about ANYTHING is possible with a computer (given sufficient I/O hardware) but much is just not worth the bother. Using easy, common, and efficient methods makes a program better to run and better to maintain.
2007-08-04 04:56:25
·
answer #3
·
answered by oldguy 4
·
1⤊
3⤋
No "we" cannot iterate through a two-dimensional array—and access each element—by using only one loop. It doesn't matter what programming language you use.
In order to process each element do the following:
Loop through each row. In order to process the columns in each row, you must also loop through them as well. That makes a total of two loops. So. you have an outer loop that processes the row access; this outer loop is processed just once. In addition, you have an inner loop that runs once for each row that is processed.
2007-08-04 05:01:14
·
answer #4
·
answered by Einstein 5
·
0⤊
4⤋
Computer Tutorials, Interview Question And Answer
http://freshbloger.com/
java architecture tutorial
http://java3.blogspot.com/
Java tutorials- http://java-tip.org/
2007-08-04 05:10:48
·
answer #5
·
answered by aerokan a 3
·
0⤊
3⤋