2012-07-17 53 views
0

此数组中的搜索号码是代码:保持在java中

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正如你所看到的,还有另外12阵列3.我怎样才能让它继续,并寻找第二个12?我不希望它终止在第一个12. 谢谢你。

回答

0

而不是break search;使用continue search;。如果您使用的是break,则会打破整个循环,而continue仅打破当前迭代并继续循环。

+0

啊。谢啦!! :D – iG0tB0lts 2012-07-17 14:03:53

+0

欢迎您:) – 2012-07-17 14:04:25

+0

如果您查看代码,该更改将与打印语句有关 – 2012-07-17 14:05:41

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

    }