簡體   English   中英

幫助搜索2D數組(Java)

[英]Help Searching a 2D Array (Java)

我完全迷失了。 我已經搜索了這個問題可能30分鍾了,我找不到任何特定於Java的解決方案(或任何與java交叉的解決方案)。

我已經嘗試使用通用的兩個簡單for循環來遍歷每個單獨的String,但是如果搜索項位於數組的第一列,它似乎只返回結果。

    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是所述對象中的2D數組。 (是的,列數總是4)

如果有任何其他信息,您可以隨意詢問。

你的搜索方法工作正常。 我創建了一個類並使用了你的搜索方法(復制和粘貼,我甚至沒有更改你的搜索),它的工作原理。 這讓我相信問題在於您輸入數據的方式。

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>

如果我們使用高級搜索方法(我稱之為search2),我們會得到:

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>

這是高級搜索方法:

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

你的“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;
    }
  }
}

你確定db.bookdb.length不總是1嗎?

這可能對您有所幫助:

int [] [] matrix = new int [10] [30]; System.out.println(“行數=”+ matrix.length); System.out.println(“列數=”+矩陣[0] .length);

我不確定你的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;
}

你需要打破這兩個循環。 您可以使用“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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM