【问题标题】:Keep searching for a number in an Array in java继续在java中的数组中搜索一个数字
【发布时间】:2012-07-17 13:58:58
【问题描述】:

这是代码:

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 点终止。 谢谢。

【问题讨论】:

    标签: java arrays search for-loop int


    【解决方案1】:

    使用continue search; 代替break search;。如果您使用break,它会中断整个循环,而continue 只会中断当前迭代并继续循环。

    【讨论】:

    • 如果您查看代码,该更改将与打印语句有问题
    • 是的,它会,但问题的关键是如何继续搜索,而不是如何修复打印。
    【解决方案2】:

    按以下方式更改您的代码

       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");
                }
        }
    

    【讨论】:

      【解决方案3】:

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

           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");
               }
      

      【讨论】:

        【解决方案4】:

        当您第一次点击要搜索的号码时,您将跳出标记为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 + ".");
        }
        

        【讨论】:

          【解决方案5】:

          我假设您正在尝试查找找到 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);
                      }
                  }
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-02-03
            • 2020-08-22
            • 2011-08-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多