2011-03-29 73 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 
}