I have,
int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, index = 0;
as shown here, we create the the two dimensional one from the origin. But how do I iterate my oneDim
inside for (index = 0; index < 10; index++)
so that I could get my column index and row index there without creating a new one?
I want it looks like this while printing its indexes to a two dimensional array (2x5):
0,0
0,1
1,0
1,1
2,0
2,1
3,0
3,1
4,0
4,1
I think the main issue here is getting the column index and row index without creating the two dimensional one. Don't you?
-
Huh? You want a one-dimensional array to look like a two-dimensional array?Simon Righarts– Simon Righarts2009年11月30日 02:53:22 +00:00Commented Nov 30, 2009 at 2:53
-
dont quite get what you're after but, create an array of objects with column index and row index?Euclid– Euclid2009年11月30日 02:55:01 +00:00Commented Nov 30, 2009 at 2:55
-
@Simon Righarts: This is standard. The compiler does it for you everyday.jason– jason2009年11月30日 03:49:53 +00:00Commented Nov 30, 2009 at 3:49
-
tomiko, as you can see people are struggling to understand your question, not struggling to solve your problem. please review your question so that we don't have to strain our brains to understand you. i'm sure what you're trying to do is very simple, once you take the time to articulate your self i'm sure you'll get some meaningful help.pstanton– pstanton2009年11月30日 06:00:25 +00:00Commented Nov 30, 2009 at 6:00
-
Okay psatanton. All that I want to ask is: "How do I transform/convert my one-dimensional array to a two-dimensional array?"van_tomiko– van_tomiko2009年11月30日 14:17:57 +00:00Commented Nov 30, 2009 at 14:17
3 Answers 3
If you want row-major order, given row rowIndex
, column columnIndex
and are faking (for lack of a better term) a two-dimensional array with numberOfColumns
columns, the formula is
rowIndex * numberOfColumns + columnIndex.
If you want row-major order, given row rowIndex
, column columnIndex
and are faking (for lack of a better term) a two-dimensional array with numberOfRow
rows, the formula is
columnIndex * numberOfRows + rowIndex.
So, assuming row-major order:
int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int rows = 2;
int columns = 5;
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
System.out.println(row + ", " + column + ": " + oneDim[row * columns + column]);
}
}
Output:
0, 0: 1
0, 1: 2
0, 2: 3
0, 3: 4
0, 4: 5
1, 0: 6
1, 1: 7
1, 2: 8
1, 3: 9
1, 4: 10
And if you insist on indexing using a single for
loop, assuming row-major order, the formula that you want is the following:
int column = index % numberOfColumns;
int row = (index - column) / numberOfColumns;
If you're using column-major order, the formula that you want is the following:
int row = index % numberOfRows;
int column = (index - row) / numberOfRows;
So,
int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int rows = 2;
int columns = 5;
for(int index = 0; index < 10; index++) {
int column = index % columns;
int row = (index - column) / columns;
System.out.println(row + ", " + column + ": " + oneDim[index]);
}
will output
0, 0: 1
0, 1: 2
0, 2: 3
0, 3: 4
0, 4: 5
1, 0: 6
1, 1: 7
1, 2: 8
1, 3: 9
1, 4: 10
as expected.
1 Comment
The two numbers you're showing could be computed, in the order you're showing them in, as index/2
and index%2
respectively. Is that what you mean by "the issue"?
Comments
I think this is what your trying to do...convert a one-dim array into a two-dim array.
//this is just pseudo code...not real syntax
int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int first_dim = 5;
int second_dim = 2;
int[first_dim][second_dim] new_array;
for (int fdi = 0; fdi < first_dim; fdi++){
for (int sdi = 0; sdi < second_dim; sdi++) {
//this is the crux...you're calculating the one dimensional index to access the value
new_array[fdi][sdi] = oneDim[fdi*second_dim + sdi]
}
}