gpt4 book ai didi

java - 帮助搜索二维数组 (Java)

转载 作者:行者123 更新时间:2023-11-29 03:57:30 25 4
gpt4 key购买 nike

我完全迷路了。我已经为这个问题搜索了可能 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)

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

最佳答案

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

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

关于java - 帮助搜索二维数组 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5476816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com