【问题标题】:Help Searching a 2D Array (Java)帮助搜索二维数组 (Java)
【发布时间】:2011-07-25 12:16:09
【问题描述】:

我完全迷路了。我已经在谷歌上搜索了这个问题可能 30 分钟,但我找不到任何特定于 java 的解决方案(或任何交叉到 java 的解决方案)。

我尝试使用通用的两个简单 for 循环来遍历每个单独的字符串,但它似乎只在搜索词位于数组的第一列时才返回结果。

    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)

如果你们需要任何其他信息,请随时询问。

【问题讨论】:

  • 您的搜索没有中断。我对其进行了测试(见下文。)我的猜测是您的bookdb 的输入已损坏,始终将数据放在第一列。

标签: java arrays search multidimensional-array


【解决方案1】:

您的搜索方法效果很好。我创建了一个类并使用了您的搜索方法(复制并粘贴,我什至没有更改您的搜索)并且它有效。这让我相信问题在于您输入数据的方式。

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

【讨论】:

  • 我仔细检查了我的数组,发现字符串中有多余的空格。哦。谢谢,感谢您的帮助。
  • @pdechuler 确定。我将对上面的代码进行两次编辑,然后粘贴结果。询问您的教授是否可以使用它们,因为有些人会对此感到不满。如果他说没关系,我会用它。如果他说不行,我会将 iluxa 的答案合并到您的循环中。
  • @pdechuler 我进行了更改。你明白它的作用吗?一旦它找到一个可接受的匹配,它就会退出循环(通过打破outer_loop。)这意味着你现在找到了数组的第一个结果而不是最后一个。因此,如果您在第一个结果中找到匹配项,则不会搜索整个数组。另一种选择是始终搜索数组并找到所有匹配的元素。我也会编辑那个。
  • @pdechuler 添加了高级搜索
  • @pdechuler 老实说,它们只是细微的调整。你原来的搜索方法很好。当您正在查看 Java 入门课程时,呵呵……如果它有效,那就对了。当我还在上大学的时候,我帮助一个好友完成了一些 CS101 作业,而且……我很惊讶。简直惊呆了。
【解决方案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) 你搜索整个数组,所以你找到了最后一个结果,而不是第一个,2) 你搜索了整个数组,所以它需要更长的时间。它只会在同一个词多次存在的情况下产生不同的输出。
  • 我应该澄清一下,这并不意味着这不是体面的建议,它只是不会解决 OPs 问题。我还应该指出,您可以使用命名循环并摆脱它。 outer_loop: for(int i = 0... 及以后的break outer_loop;
  • 感谢您发现这一点,但它仍然不能修复错误(正如glowcoder 指出的那样)。而且速度不是问题,最多只能搜索由少于 20 个字符组成的 12x4 字符串数组。另外,我假设只有一种可能的结果。
【解决方案3】:

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

这可能会对您有所帮助:

int[][] 矩阵 = 新的 int[10][30]; System.out.println("行数 = " + matrix.length); System.out.println("列数 = " + matrix[0].length);

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      你需要打破这两个循环。您可以用“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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-26
        • 1970-01-01
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-31
        相关资源
        最近更新 更多