2011-03-29 48 views
1

我完全迷失了。我已经为这个问题搜索了30分钟,我找不到任何java特定的解决方案(或任何跨越java的解决方案)。帮助搜索2D阵列(Java)

我试过使用泛型两个简单的循环来检查每个单独的字符串,但它似乎只返回结果如果搜索项在数组的第一列。

public static String search(String term) { 
     String result = ""; 
     int row = db.bookdb.length; 

     for (int i=0; i<db.bookdb.length; i++) { 
      for (int j=0; j<4; j++) { 
       if (term.equals(db.bookdb[i][j])) { 
        row = i; 
        break; 
       } 
      } 
     } 

     if (row == db.bookdb.length) { 
      result += "Your search failed to return any results"; 
     } 
     else { 
      for (int j=0; j<4; j++) { 
       result += db.bookdb[row][j] + " "; 
      } 
     } 

     return result; 
    } 

db是我正在使用的对象,bookdb是所述对象中的二维数组。 (是的,列的数量将永远是4)

如果有任何额外的信息,你们需要随时问。

+0

您的搜索没有被打破。我测试了它(见下文)。我的猜测是,你的'bookdb'输入被破坏,总是把数据放在第一列。 – corsiKa 2011-03-29 18:17:38

回答

2

您的搜索方法正常运行。我创建了一个类并使用了你的搜索方法(COPIED AND PASTED,我甚至没有改变你的搜索)并且它可以工作。这使我相信问题在于你如何输入数据。

class Pdeuchler { 

    static Pdeuchler db; 

    String[][] bookdb; 


    public static String search(String term) { 
     String result = ""; 
     int row = db.bookdb.length; 

     outer_loop: // CHANGE #1 added a named loop 
     for (int i=0; i<db.bookdb.length; i++) { 
      for (int j=0; j<4; j++) { 
       if (term.equals(db.bookdb[i][j])) { 
        row = i; 
        //break; //REMOVED 
        break outer_loop; // CHANGE #2 breaking to the outer_loop 
       } 
      } 
     } 

     if (row == db.bookdb.length) { 
      result += "Your search failed to return any results"; 
     } 
     else { 
      for (int j=0; j<4; j++) { 
       result += db.bookdb[row][j] + " "; 
      } 
     } 

     return result; 
    } 

    public static void main(String[] args) { 
     db = new Pdeuchler(); 
     db.bookdb = new String[10][4]; // title, author, publisher, year 

     db.bookdb[0] = new String[] {"Awesome Book","Stan","West","2001"}; 
     db.bookdb[1] = new String[] {"Cool Story","Dan","North","2002"}; 
     db.bookdb[2] = new String[] {"Brothers","North","North","2003"}; 
     db.bookdb[3] = new String[] {"Never again!","Bob","West","2004"}; 
     db.bookdb[4] = new String[] {"Howdy Partner","Stan","South","2005"}; 
     db.bookdb[5] = new String[] {"What the StackOverflow?","Dan","North","2006"}; 
     db.bookdb[6] = new String[] {"That's hilarious","Angie","South","2007"}; 
     db.bookdb[7] = new String[] {"I like pie","Angie","East","2008"}; 
     db.bookdb[8] = new String[] {"Bob writes a book","Bob","South","2009"}; 
     db.bookdb[9] = new String[] {"The adverntures of Bob","Bob","North","2010"}; 

     System.out.println(search("I like pie")); 
     System.out.println(search("North")); 
     System.out.println(search("Dan")); 
    } 

} 

而且结果:

C:\junk>java Pdeuchler 
I like pie Angie East 2008 
The adverntures of Bob Bob North 2010 
What the StackOverflow? Dan North 2006 

C:\junk> 

,并与变化的结果:

C:\junk>javac Pdeuchler.java 

C:\junk>java Pdeuchler 
I like pie Angie East 2008 
Cool Story Dan North 2002 
Cool Story Dan North 2002 

C:\junk> 

如果我们采用了先进的搜索方法(我打电话搜索2),我们得到这个:

C:\junk>java Pdeuchler 
simple search: 
I like pie Angie East 2008 
Cool Story Dan North 2002 
Cool Story Dan North 2002 

advanced search: 
I like pie Angie East 2008 

Cool Story Dan North 2002 
Brothers North North 2003 
What the StackOverflow? Dan North 2006 
The adverntures of Bob Bob North 2010 

Cool Story Dan North 2002 
What the StackOverflow? Dan North 2006 


C:\junk> 

这里的先进海rch方法:

public static String search2(String term) { 
    String result = ""; 
    int row = db.bookdb.length; 

    for (int i=0; i<db.bookdb.length; i++) { 
     for (int j=0; j<4; j++) { 
      if (term.equals(db.bookdb[i][j])) { 
       row = i; 
       for (int k=0; k<4; k++) { 
        result += db.bookdb[i][k] + " "; 
       } 
       result += "\n"; 
       break; // breaks out of the INNER (j) loop 
      } 
     } 
    } 

    if (row == db.bookdb.length) { 
     result += "Your search failed to return any results"; 
    } 
    return result; 
} 
+0

我仔细检查了我的数组,并且我有多余的空格包围字符串。 D'哦。谢谢,我感谢帮助。 – pdeuchler 2011-03-29 18:22:10

+0

@pdechuler肯定的事情。我将对上面的代码进行两次编辑,然后粘贴结果。问问你的教授是否可以接受使用,因为有些人会皱眉。如果他说没关系,我会用它。如果他说不好,我会将iluxa的答案加入你的循环中。 – corsiKa 2011-03-29 18:24:49

+0

@pdechuler我做了更改。你看到它做了什么?只要它找到可接受的匹配,就会从循环中取出(通过破坏outer_loop)。这意味着您现在可以找到数组的第一个结果而不是LAST。因此,如果您在第一个结果中找到匹配项,则不会搜索整个数组。另一种选择是总是搜索数组并找到所有匹配的元素。我也会编辑那一个。 – corsiKa 2011-03-29 18:28:04

0

你确定db.bookdb.length不总是1吗?

这可以帮助你:

INT [] []矩阵=新INT [10] [30]; System.out.println(“Number of rows =”+ matrix.length); System.out.println(“Number of columns =”+ matrix [0] .length);

2

你的“break”语句突破了内部循环而不是外部循环。再这样写:

boolean found = false; 

for (int i = 0; i < db.bookdb.length && !found; i ++) { 
    for (int j=0; j<4 && !found; j++) { 
    if (yourCondition) { 
     row = i; 
     found = true; 
    } 
    } 
} 
+1

这样做的唯一后果是1)你搜索整个数组,所以你找到最后的结果,而不是第一个,2)你搜索整个数组,所以它需要更长的时间。只会在同一个词多次存在的情况下才会产生差异。 – corsiKa 2011-03-29 18:07:06

+1

我应该澄清,这并不意味着这是不体面的建议,它只是不会解决OPs问题。我还应该指出,你可以使用一个命名的循环并打破它。 'outer_loop:for(int i = 0 ...'和后面的'break outer_loop;' – corsiKa 2011-03-29 18:08:20

+0

谢谢你接触这个,但它仍然没有修复这个bug(正如glowcoder指出的那样)。速度不是问题,最多只能搜索一个由小于20个字符组成的12x4字符串数组,而且我假设只有一个可能的结果。 – pdeuchler 2011-03-29 18:14:12

0

我不知道你的bookdb的类型,但你可以改变它

private static int searchTermInArray(String[][] bookdb, String term) { 
    for (int i=0; i<bookdb.length; i++) { 
     for (int j=0; j<4; j++) { 
      if (term.equals(bookdb[i][j])) { 
       return i; 
      } 
     } 
    } 
    return -1; 
} 

public static String search(String term) { 
    String result = ""; 
    int row = searchTermInArray(db.bookdb, term); 
    if (row == -1) { 
     result += "Your search failed to return any results"; 
    } 
    else { 
     for (int j=0; j<4; j++) { 
      result += db.bookdb[row][j] + " "; 
     } 
    } 

    return result; 
} 
0

你需要打破这两个循环。你可以用“mainfor”来标注外部循环,并在休息之后使用该标签

public static String search(String term) { 
    String result = ""; 
    int row = db.bookdb.length; 

    mainfor: for (int i=0; i<db.bookdb.length; i++) { 
     for (int j=0; j<4; j++) { 
      if (term.equals(db.bookdb[i][j])) { 
       row = i; 
       break mainfor; 
      } 
     } 
    } 
... //rest of your code as is 
}