简体   繁体   English

遍历2D数组; 魔术广场

[英]Iterating through a 2D array; magic square

For my summer Java class, I'm working on a program that detects whether the elements in an array are a magic square. 对于我的夏季Java课,我正在开发一个程序,该程序可检测数组中的元素是否为幻方。 I'm not entirely sure how to properly iterate through the array to be able to calculate the total of the separate columns and rows to determine if the array is a magic square. 我不完全确定如何正确地遍历数组,以便能够计算出单独的列和行的总数,以确定数组是否是一个魔术方块。 We were given the code for the sum of the down diagonal, but I'm unsure how to approach the up diagonal, rows and columns. 我们得到了向下对角线总和的代码,但是我不确定如何逼近向上对角线,行和列。 Here is a snippet of my code so far. 到目前为止,这是我的代码片段。

public class MagicSqaure
{
    public static void main(String[] args)
    {
        int[][] array = 
        {
            {2, 7, 6},
            {9, 5, 1},
            {4, 3, 8}
        };  

public static int getSumOfDownDiagonal(int[][] array)
{
    int total = 0;
    for (int i = 0; i < array.length; i++)
    {
        total += array[i][i];
    }
    return total;
}       

Since it is a class, I can only provide help on how to accomplish traversing through the rows, columns and up diagonal, but providing **no code at all*. 由于这是一个类,因此我只能提供有关如何完成行,列和对角线遍历的帮助,但是“根本不提供代码”。 It's up to you to do the right implementation, otherwise you're not learning but copying/pasting/becoming a zombie coder. 正确的实现取决于您,否则您不是在学习而是在复制/粘贴/成为僵尸编码器。

  • To traverse each row, go through the elements of your array of arrays like this: 要遍历每一行,请像这样遍历数组的元素:

     total += array[i][j]; 

    Where i won't change but j changes. i不会改变但j改变的地方。

  • To traverse each column, go through the elements of your array of arrays like this: 要遍历每一列,请像下面这样遍历数组的元素:

     total += array[i][j]; 

    Where i changes but j won't change. i在哪里改变,但j不会改变。

  • To traverse the up diagonal, go through the elements of your array of arrays like this: 要遍历对角线,请像这样遍历数组的元素:

     total += array[i][j]; 

    Where i starts at the last possible index of the array of arrays and j starts at the first index of the array of arrays. 其中i从数组数组的最后一个索引开始,而j从数组数组的第一个索引开始。

Whenever I need to derive equations for certain behaviors, I just write a few of the answers out manually then look for patterns. 每当我需要为某些行为导出方程式时,我只需手动写出一些答案,然后寻找模式。 First let's assume this is how we access items in the array: 首先让我们假设这是我们访问数组中项目的方式:

       column
        0 1 2
       --------
    0 | 2 7 6
row 1 | 9 5 1
    2 | 4 3 8

(Using array[column][row])

Now let's get the indices for the columns: 现在,让我们获取列的索引:

column 0 = {2, 9, 4} = array[0][0], array[0][1], array[0][2]
column 1 = {7, 5, 3} = array[1][0], array[1][1], array[1][2]

And here are the rows: 以下是这些行:

row 0 = {2, 7, 6} = array[0][0], array[1][0], array[2][0]
row 1 = {9, 5, 1} = array[0][1], array[1][1], array[2][1]

And here's the other diagonal: 这是另一个对角线:

3x3 array = {4, 5, 6} = array[0][2], array[1][1], array[2][0]
4x4 array = array[0][3], array[1][2], array[2][1], array[3][0]

Notice any patterns? 注意到任何模式吗? For the diagonal, we start at array[0][array.length - 1] and end at array[array.length - 1][0]. 对于对角线,我们从array [0] [array.length-1]开始,到array [array.length-1] [0]结束。 This means our loop would be as follows: 这意味着我们的循环如下:

int total = 0;
for (int i = 0; i < array.length; i++)
{
    total += array[i][array.length - 1 - i];
}
return total;

And to sum a column, it'd be: 总而言之,它将是:

int total = 0;
for (int i = 0; i < array.length; i++)
{
    total += array[column_index][i];
}
return total;

And for rows: 对于行:

int total = 0;
for (int i = 0; i < array.length; i++)
{
    total += array[i][row_index];
}
return total;

EDIT: In response to downvotes and comments, I have modified the magic square code to work with row-major conventions. 编辑:为了回应不赞成票和提出的评论,我修改了魔方代码以使用行主要约定。 Oh wait, no I didn't since the code would be identical. 哦,等等,不,我没有,因为代码是相同的。 Anyway, this is how you do it. 无论如何,这就是您的做法。

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

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