Java 将一维数组迭代为二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1817631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:56:55  来源:igfitidea点击:

Iterating one dimension array as two dimension array

javaarrays

提问by van_tomiko

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 oneDiminside for (index = 0; index < 10; index++)so that I could get my column indexand row indextherewithoutcreating a new one? I want it looks like this while printing its indexes to a two dimensional array (2x5):

如图所示这里,我们创建一个从原点的二维之一。但是我如何迭代我的oneDim内部,for (index = 0; index < 10; index++)以便我可以创建新索引的情况下获得我的列索引行索引?我希望在将其索引打印到二维数组(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 indexand row indexwithout creating the two dimensional one. Don't you?

我认为这里的主要问题是在不创建二维索引的情况下获取列索引行索引。不是吗?

采纳答案by jason

If you want row-major order, given row rowIndex, column columnIndexand are faking (for lack of a better term) a two-dimensional array with numberOfColumnscolumns, the formula is

如果你想要行优先顺序,给定 row rowIndex, columncolumnIndex并且伪造(由于缺乏更好的术语)带有numberOfColumns列的二维数组,则公式为

rowIndex * numberOfColumns + columnIndex.

If you want row-major order, given row rowIndex, column columnIndexand are faking (for lack of a better term) a two-dimensional array with numberOfRowrows, the formula is

如果你想要行优先顺序,给定 row rowIndex, columncolumnIndex并且伪造(由于缺乏更好的术语)带有numberOfRow行的二维数组,则公式为

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 forloop, assuming row-major order, the formula that you want is the following:

如果您坚持使用单个for循环进行索引,假设行优先顺序,您想要的公式如下:

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.

正如预期的那样。

回答by Alex Martelli

The two numbers you're showing could be computed, in the order you're showing them in, as index/2and index%2respectively. Is that what you mean by "the issue"?

但您是可以计算的两个数字,在为了你展示他们,因为index/2index%2分别。这就是你所说的“问题”吗?

回答by milesmeow

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] 

   }
}