简体   繁体   English

帮助搜索2D数组(Java)

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

I'm completely lost. 我完全迷失了。 I've googled for this problem for possibly 30 minutes now, and I can't find any java specific solutions (or any solutions that crossover into java). 我已经搜索了这个问题可能30分钟了,我找不到任何特定于Java的解决方案(或任何与java交叉的解决方案)。

I've tried using the generic two simple for loops for going through each individual String, but it only seems to return results if the search term is in the first column of the array. 我已经尝试使用通用的两个简单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 is the object i'm working with, and bookdb is the 2D array within said object. db是我正在使用的对象,bookdb是所述对象中的2D数组。 (and yes, the amount of columns will always be 4) (是的,列数总是4)

If there's any additional information you guys need feel free to ask. 如果有任何其他信息,您可以随意询问。

Your search method works fine. 你的搜索方法工作正常。 I created a class and used your Search method (COPIED AND PASTED, I didn't even change your search) and it works. 我创建了一个类并使用了你的搜索方法(复制和粘贴,我甚至没有更改你的搜索),它的工作原理。 This leads me to believe the problem is in how you're entering your data. 这让我相信问题在于您输入数据的方式。

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

}

And the results: 结果如下:

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>

and the results with the change: 以及改变的结果:

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>

If we use an advanced search method (which I'm calling search2) we get this: 如果我们使用高级搜索方法(我称之为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>

Here's the advanced search method: 这是高级搜索方法:

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

your "break" statement breaks out of the inner loop but not outer one. 你的“break”语句会突破内部循环而不是外部循环。 re-write like this: 像这样重写:

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

Are you sure db.bookdb.length isn't always 1? 你确定db.bookdb.length不总是1吗?

This might help you: 这可能对您有所帮助:

int[][] matrix = new int[10][30]; int [] [] matrix = new int [10] [30]; System.out.println("Number of rows = " + matrix.length); System.out.println(“行数=”+ matrix.length); System.out.println("Number of columns = " + matrix[0].length); System.out.println(“列数=”+矩阵[0] .length);

I'm not sure what your bookdb is type but you can change it 我不确定你的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;
}

You need to break out of both the loops. 你需要打破这两个循环。 You can label the outer loop with say "mainfor" and use that label after the break 您可以使用“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