简体   繁体   English

搜索二维int [] []数组?

[英]searching a 2D int[][] array?

How do I search a 2D array for a specific number (1)? 如何在2D数组中搜索特定数字(1)? I thought the following code did it, but apparently I was looking in a specific spot whenI declared it with [4][4]. 我以为下面的代码可以做到这一点,但是当我用[4] [4]声明它时,显然我正在寻找特定的位置。

    boolean undirectedCircuit (int [][] graph)
{
    //graph = graph1(graph1(null));

    int edgeCounter = 0;

    for (int edge = 0; edge < graph.length; edge++)
    {
        /* SET FOR WHEN 1s are found in array: edgeCounter++;*/
        if(graph[4][4] == '1')
        {
            edgeCounter++;
            System.out.println("edgeCounter found '1' " + edgeCounter + "times");
        }
    }

    if (edgeCounter % 2 == 0)
    {
        System.out.println("This is a circuit!");
        //return true;
    }
    else System.out.println("This is not a circuit!!");
    return false;
    }

public void go ()
{
    graph1 = new int[][] //This line is complained about.
            {
            {0,1,1,1,0},
            {1,0,0,0,1},
            {1,0,0,1,0},
            {1,0,1,0,1},
            {0,1,0,1,0}
            };

    undirectedCircuit(graph1); //This is complained about.
} 

This is part of an assignment from my school, just pointers would be great. 这是我学校分配的作业的一部分,只是指针会很棒。 Thank you! 谢谢!

You could try something like this where you will iterate through both dimensions of the array and check your current location rather than the 4,4 您可以尝试执行类似的操作,在其中遍历数组的两个维度并检查当前位置,而不是4,4

for (int x = 0; x < graph.length; x++)
        {
            for (int y = 0; y < graph[x].length; y++)
            {
                /* SET FOR WHEN 1s are found in array: edgeCounter++;*/
                if (graph[x][y] == 1)
                {
                    edgeCounter++;
                    System.out.println("edgeCounter found '1' " + edgeCounter + "times");
                }
            }
        }

This line is wrong in two ways: 此行有两种错误的方式:

 if(graph[4][4] == '1')
  1. The quotes around '1' make it a char literal. '1'周围的引号使它成为char文字。 Since your array contains int s, you'll want to drop the quotes and just write 1 . 由于您的数组包含int ,因此您需要删除引号并只写1

  2. graph[4][4] will always check the same value in the array as you said. graph[4][4]将始终检查数组中与您说的值相同的值。 Specifically, it will always access the fifth value in the fifth array of your 2d array. 具体来说,它将始终访问2d数组的第五个数组中的第五个值。 Whenever you write numbers in your code, they are constants: the number 4 is never going to change during your program's execution, so you keep using 4 as the index over and over again, accessing the fifth element each time you do so. 每当您在代码中写入数字时,它们都是常量:数字4在程序执行期间永远不会改变,因此您将一遍又一遍地使用4作为索引,每次这样做都访问第五个元素。

In order to access every element in an array, you can loop over it like this: 为了访问数组中的每个元素,您可以像这样遍历它:

for (int n = 0; n < array.length; n ++)
{
    array[n]; //access the nth element of the array
}

n in this instance is the same as your edge variable. 在这种情况下, n与您的edge变量相同。

However, since you are using a 2d array, these elements are themselves arrays! 但是,由于您使用的是2d数组,因此这些元素本身就是数组! Therefore, you need another loop: 因此,您需要另一个循环:

//array is a 2d array...

for (int n = 0; n < array.length; n ++)
{
    //...so its elements are 1d arrays
    for (int m = 0; m < array[n].length; m ++)
    {
        array[m][n]; //here we have a specific object in our 2d array.
    }
}

We use variables for our indices so they can change in the loop and access different values in the array. 我们使用变量作为索引,以便它们可以在循环中更改并访问数组中的不同值。 Hope this helps! 希望这可以帮助!

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

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