简体   繁体   English

减少Java中的多维数组

[英]Decrements a multidimensional array in java

I'm having trouble understanding how to start the first index of array from the bottom to to the top of a multidimensional array. 我在理解如何从多维数组的底部到顶部开始数组的第一个索引时遇到麻烦。 Here's what I've tried to initialize my array from bottom to top (in 2d array table format): 这是我尝试从底部到顶部初始化数组(以2d数组表格式)的内容:

  for(int row = arrayName.length - 1; row > 0; row--) {
     for(int col = 0; col < arrayName.length; col++) {
        arrayName[row][col] = ' ';
     }
  }      

or 要么

for(int row = arrayName.length - 1; row > 0; row--) {
  for(int col = arrayName.length - 1; col < 0; col--) {
     arrayName[row][col] = ' ';
  }
}      

i mean..when i run the program, the array always store my values from top to bottom, the opposite of what i wanted it to do. 我的意思是..当我运行程序时,数组总是从上到下存储我的值,这与我想要执行的操作相反。 Please help! 请帮忙! thanks in advance. 提前致谢。

Matrices and Arrays don't really have any inherent sense of direction. 矩阵和数组实际上并没有任何固有的方向感。 Their direction is entirely determined by the order in which you choose to display the information. 它们的方向完全取决于您选择显示信息的顺序。 Depending on how you structure your loop, the contents of the array will be outputted differently. 根据构造循环的方式,数组的内容将以不同的方式输出。 If you want to store elements from the "bottom" to the "top," what that actually means is that your print out loop should be structured in an opposite direction from your assignment loop. 如果要存储从“底部”到“顶部”的元素,这实际上意味着打印输出循环的结构应与分配循环的方向相反。 So for example (assuming an NxN array), 因此,例如(假设一个NxN数组),

for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr.length; j++) {
        arr[i][j] = <some value here>;
    }
}

Say for simplicity's sake you think of vertex (0,0) being in the top left corner, where columns increase as you move right and rows increase as you move down. 为简单起见,您认为顶点(0,0)位于左上角,其中,当您向右移动时,列会增加,而当您向下移动时,行会增加。 Then to print from the bottom up, you would want to start from the last row and move to the first row. 然后,要从下往上打印,您需要从最后一行开始,然后移至第一行。

for (int i = arr.length - 1; i >= 0; i--) { // Note the >= 0, which was incorrect
                                            // in the code you posted
    for (int j = 0; j < arr.length; j++) {
        System.out.print(arr[i][j]+" ");
    }
    System.out.println();
}

That would start from the bottom row and print out its contents from the first to last column, then move up to the second to last row, etc. all the way up to the first (top) row. 这将从下一行开始,并从第一列到最后一列打印其内容,然后向上移动到第二列至最后一行,依此类推,一直到第一行(顶部)。 So there is no inherent directionality in arrays at all, but because we are printing in the opposite order from the one used in the value assignment, the values will appear to be printed from the "bottom up." 因此,数组中根本没有固有的方向性,但是由于我们的打印顺序与值分配中所使用的顺序相反,因此这些值似乎是从下而上打印的。 If you wanted to also invert the columns, printing the last one first, you could do this: 如果您还想反转列,首先打印最后一列,则可以执行以下操作:

for (int i = arr.length - 1; i >= 0; i--) {
    for (int j = arr.length - 1; j >= 0; j--) {
        System.out.print(arr[i][j]+" ");
    }
    System.out.println();
}

I hope that explanation was clear. 我希望解释清楚。 Best of luck~! 祝你好运〜!

PS. PS。 As there is no inherent directionality in arrays, you should not necessarily be storing the values in the direction you want to print them. 由于数组中没有固有的方向性,因此不必一定要在要打印它们的方向上存储值。 You should store the values in the way that makes the most sense to you and in the way that makes them easy to manage. 您应该以对您最有意义的方式和易于管理的方式存储值。 Then whenever you need to print the values, you can print them however you like. 然后,每当需要打印值时,都可以根据需要打印它们。

Try this code, but I see that you are filling the array with empty spaces, how can you realize that your code is not working good try for example filling numbers to test your code. 尝试使用此代码,但是我看到您正在用空白填充数组,如何才能意识到自己的代码无法正常工作,请尝试使用例如填充数字来测试代码。

for(int row = arrayName.length -1; row >= 0; row--){
    for(int col = arrayName.length - 1; row >= 0; col--){
       arrayName[row][col] = col;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM