0

这是代码:

public class test2 {
    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        int[][] arrayOfInts = { 
                { 32, 87, 3, 589 },
                { 622, 1076, 2000, 8 },
                { 12, 127, 77, 955 },
                {12, 3}
        };
        int searchfor = 12;

        int i;
        int j = 0;
        boolean foundIt = false;

        search:
            for (i = 0; i < arrayOfInts.length; i++) {
                for (j = 0; j < arrayOfInts[i].length;
                        j++) {
                    if (arrayOfInts[i][j] == searchfor) {
                        foundIt = true;
                        break search;
                    }
                }
            }

        if (foundIt) {
            System.out.println("Found " + searchfor +
                    " at " + i + ", " + j);
        } else {
            System.out.println(searchfor +
                    " not in the array");
        }
    }
}

此代码在前 12 处停止。如您所见,数组 3 中还有另一个 12。我怎样才能让它继续下去,并搜索第二个 12?我不希望它在前 12 点终止。谢谢。

4

5 回答 5

0

我假设您正在尝试查找找到 searchFor 的所有位置。这是方法之一。此代码不使用任何标签。这将打印找到 searchFor 的所有位置。

public static void main(String[] args) throws InterruptedException {
        int[][] arrayOfInts = { { 32, 87, 3, 589 }, 
                                { 622, 1076, 2000, 8 }, 
                                { 12, 127, 77, 955 }, 
                                { 12, 3 } };
        int searchfor = 12;

        int i;
        int j = 0;
        boolean foundIt = false;
        for (i = 0; i < arrayOfInts.length; i++) {
            foundIt = false;
            for (j = 0; j < arrayOfInts[i].length; j++) {
                if (arrayOfInts[i][j] == searchfor) {
                    foundIt = true;
                    continue;
                }
            }
            if (foundIt) {
                    System.out.println("Found " + searchfor + " at " + i + ", " + j);
            }
            else {
                System.out.println(searchfor + " not in the array "+i);
            }
        }

    }
于 2012-07-17T14:33:16.983 回答
0

通过以下方式更改您的代码

   public static void main(String[] args) throws InterruptedException {
        int[][] arrayOfInts = { 
                { 32, 87, 3, 589 },
                { 622, 1076, 2000, 8 },
                { 12, 127, 77, 955 },
                {12, 3}
            };
            int searchfor = 12;

            int i;
            int j = 0;
            boolean foundIt = false;
            int foundItIndexI = null;
            int foundItIndexJ = null;

            for (i = 0; i < arrayOfInts.length; i++) {
                for (j = 0; j < arrayOfInts[i].length;
                     j++) {
                    if (arrayOfInts[i][j] == searchfor) {
                        foundIt = true;
                        foundItIndexI = i;
                        foundItIndexJ = j;

                    }
                }
            }

            if (foundIt) {
                System.out.println("Found " + searchfor +
                                   " at " + foundItIndexI + ", " + foundItIndexJ);
            } else {
                System.out.println(searchfor +
                                   " not in the array");
            }
    }
于 2012-07-17T14:05:33.983 回答
0

这将跟踪找到的每个坐标:

     int[][] arrayOfInts = { 
             { 32, 87, 3, 589 },
             { 622, 1076, 2000, 8 },
             { 12, 127, 77, 955 },
             {12, 3}
         };
         int searchfor = 12;

         int i;
         int j = 0;


         // this is dumb way to keep track of coordinates, but prob
         // not important to have better implementation (eg. Coordinate object)
         List<String> foundCoordinates = new ArrayList<String>();

     search:
         for (i = 0; i < arrayOfInts.length; i++) {
             for (j = 0; j < arrayOfInts[i].length;
                  j++) {
                 if (arrayOfInts[i][j] == searchfor) {
                     foundCoordinates.add(i + ", " + j);
                     continue search;
                 }
             }
         }

         if (foundCoordinates.size() > 0) {
             System.out.println("Found " + searchfor +
                                " at:" );

             for(String s : foundCoordinates)
             {
                 System.out.println(s);
             }
         } else {
             System.out.println(searchfor +
                                " not in the array");
         }
于 2012-07-17T14:06:03.513 回答
0

search当您第一次点击您正在搜索的数字时 ,您正在打破标记的循环。

如果我猜对了你想要完成的事情,我会这样做

boolean foundIt = false;

for (int i = 0; i < arrayOfInts.length; i++) {
    for (int j = 0; i < arrayOfInts.length; j++) {
        if (arrayOfInts[i][j] == searchFor) {
            foundIt = true;
            System.out.println("Found " + searchFor + " at [" + i + ", " + j + "].");
        }
    }
}

if (foundIt == false) {
    System.out.println("Haven't found " + searchFor + ".");
}

如果你想记住你找到它们的位置,你可以使用java.awt.Point或者你可以为此编写自己的类。

您的代码将如下所示:

boolean foundIt = false;
List<Point> points = new ArrayList<Point>();

for (int i = 0; i < arrayOfInts.length; i++) {
    for (int j = 0; i < arrayOfInts.length; j++) {
        if (arrayOfInts[i][j] == searchFor) {
            foundIt = true;
            points.add(new Point(j, i));
            System.out.println("Found " + searchFor + " at [" + i + ", " + j + "].");
        }
    }
}

if (foundIt == false) {
    System.out.println("Haven't found " + searchFor + ".");
}
于 2012-07-17T14:06:20.757 回答
0

而不是break search;使用continue search;. 如果您使用break,它会中断整个循环,同时continue仅中断当前迭代并继续循环。

于 2012-07-17T14:01:17.187 回答