我试图在char [] []数组中搜索字符串。
我认为我的代码的问题是当charArray [k]被发现匹配时,charArray [k + 1]需要匹配puzzle [i] [j + 1]并继续匹配整个charArray 。长度。
但是因为给定单词的长度没有预先确定,所以我不能为单词的每个值写出超级复杂的多个嵌套for循环。
另外,当我的代码找到charArray [k]时,首先它不会增加k的值,它也不会从它停止的位置开始搜索下一个字符。
我觉得解决方案可能是两种方法之间交换信息?或者以某种方式嵌套在第一个for循环中的递归方法?
请帮忙!
谢谢你!public static Boolean search(char[][] puzzle, String word) {
char[] charArray = word.toCharArray();
//search array
for(int k = 0; k < charArray.length; k ++) {
for (int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle[i].length; j++) {
if ( puzzle[i][j] == charArray[k])
continue;
}
}
}
return true;
}
答案 0 :(得分:0)
如果您只有水平字,那么此代码将执行:
public class Test {
private static char[][] puzzle = {
{ 'a' ,'z', 'e'},
{ 'a' ,'z', 'f','g'},
{ 'a' ,'z', 't', 'k','m'},
{ 'a' ,'z', 'k'}
};
public static void main(String[] args) {
System.out.println(search(puzzle, "azh"));
System.out.println(search(puzzle, "azgg"));
System.out.println(search(puzzle, "azfg"));
System.out.println(search(puzzle, "aztkm"));
}
public static Boolean search(char[][] puzzle, String word) {
for (int i = 0; i < puzzle.length; i++) {
String puzzleWord = new String(puzzle[i]);
if (word.equals(puzzleWord)) {
return true;
}
}
return false;
}
}
您还需要检查垂直字吗?
答案 1 :(得分:0)
代码有两件事。
首先,为什么你继续因为你找到了相等,你应该迭代数组。(当你继续,迭代退出循环。&#34; if(puzzle [i] [j] == charArray [K])&#34;
第二件事是你没有增加&#39; k&#39;指数。为了做出正确的比较,你应该增加两个索引。 (if(puzzle [i] [j] == charArray [k ++]))。
请尝试此代码。你不需要外循环。&#34; for(int k = 0; k&lt; charArray.length; k ++){&#34;
public static void main(String[] args) {
//Found case
char[][] puzzle = {{'b','o','o','k'}, {'a','p','p','l','e'}, {'t','a','b','l','e'}};
String word = "apple";
System.out.println("result: " + search(puzzle, word));
//Output is-> result: true
//Not Found case
char[][] puzzle2 = {{'b','o','o','k'}, {'a','p','p','l','e','e'}, {'t','a','b','l','e'}};
System.out.println("result: " + search(puzzle2, word));
//Output is-> result: false
}
public static Boolean search(char[][] puzzle, String word) {
char[] charArray = word.toCharArray();
//search array
for (int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle[i].length && j < charArray.length; j++) {
//when not equal character or lengths are not equal break loop
if ( puzzle[i][j] != charArray[j] || puzzle[i].length != charArray.length)
break;
//Equal and all characters compared
else if( j + 1 == puzzle[i].length){
return true;
}
}
}
return false;
}
答案 2 :(得分:0)
使用Stream
:
首先,迭代数组:
Arrays.stream(array)
然后,使用Arrays.equals
搜索任何匹配的内容,您需要获得char[]
的{{1}}
String
让我们使用char[] search = word.toCharArray();
告诉我们anyMatch
中是否至少有一个匹配项:
Stream
此处使用的.anyMatch(a -> Arrays.equals(a, search));
只需使用Predicate
来检查两个Arrays.equals
是否相同。这很简单,并不需要实例化char[]
。
完整代码:
String
首先,您不想直接查找单词以查找public static boolean search(char[][] array, String word){
char search = word.toCharArray();
return Arrays.stream(array)
.anyMatch(a -> Arrays.equals(a, search));
}
。对于每一行,您将迭代char[][]
。
charArray
退出条件可能会有所改善,但我不想花太多时间在那部分,因为只需使用for (int i = 0; i < puzzle.length; i++) {
//If both length don't match, this can't be good.
if(puzzle[i].length == charArray.length){
//Check both array char by char
for(int k = 0; k < charArray.length; k++) {
if (puzzle[i][j] != charArray[k])
break; //doesn't match, skip that row
}
if (charArray.length - 1 == k){ return true; }
}
}
}
//No match found
return false;
即可完成比较:
Arrays.equals
答案 3 :(得分:0)
如果String数组中的String与给定值匹配,那么这里的所有答案都匹配但是我不认为你在寻找什么:/我已经创建了一个匹配的自定义方法一个特定的单词是char [] []数组的一部分。
public static Boolean wordExistsInCharArray(char[][] puzzle, String word) {
char[] charArray = word.toCharArray();
//search array for words only! A word is defined when there is a whitespace on both sides or start/end of input.
int currentWordIndex, charArrayIndex;
for (int i = 0; i < puzzle.length; i++) {
currentWordIndex = 0;
charArrayIndex = 0;
for (int j = 0; j < puzzle[i].length; j++) {
if (puzzle[i][j] == ' ') { //word has ended and we need to check if it matches the one we are looking for.
currentWordIndex = 0;
if (charArrayIndex == charArray.length) {
return true; // all the characters in the word were presented in current puzzle row. You now have both i, j indexes.
}
charArrayIndex = 0;
} else {
currentWordIndex++; // extend current word length with one character
if (currentWordIndex - 1 == charArrayIndex) { // check if current word length and parsed characters length are equal otherwise just continue
if (charArrayIndex < charArray.length && charArray[charArrayIndex] == puzzle[i][j]) { // test if next character from charArray matches current word character
charArrayIndex++; // extend charArrayIndex if there is a match
} else {
charArrayIndex = 0; // reset charArrayIndex since there is no match or current word length is bigger than needed.
}
} else {
continue;
}
}
}
if (charArrayIndex == charArray.length) { // in the case when the puzzle[i] has ended and we did not check if we have any occurrence
return true; // all the characters in the word were presented in current puzzle row. You now have both i, j indexes.
}
}
return false;
}
然后进行测试:
char[][] puzzle1 = new char[][] {
{'f', 'o', 'o', ' ', 'b', 'a', 'r'},
{'f', 'o', 'o', ' ', 'b', 'u', 'z'},
{'f', 'o', 'o', ' ', 'f', 'i', 'g', 'h', 't', 'e', 'r'}
};
char[][] puzzle2 = new char[][]{
{'f', 'o', 'o', ' ', 'b', 'a', 'r'},
{'f', 'o', 'o', ' ', 'b', 'u', 'z'},
{'f', 'o', 'o', ' ', 'f', 'i', 'g', 'h', 't', 'e', 'r', 'e', 'u', 'r', 'o'}
};
char[][] puzzle3 = new char[][] {
{'f', 'o', 'o', ' ', 'b', 'a', 'r'},
{'f', 'o', 'o', ' ', 'b', 'u', 'z'},
{'f', 'o', 'o', ' ', 'e', 'u', 'r', 'o', 'f', 'i', 'g', 'h', 't', 'e', 'r'}
};
char[][] puzzle4 = new char[][] {
{'m', 'o', 't', 'h', ' ', 'i', 's', ' ', 'n', 'o', 't', ' ', 'a', ' ', 'r', 'e', 'a', 'l', ' ', 'w', 'o', 'r', 'd'}
};
char[][] puzzle5 = new char[][] {
{'I', ' ', 'l', 'o', 'v', 'e', ' ', 'm', 'y', ' ', 'm', 'o', 't', 'h', 'e', 'r', ' ', 'f', 'o', 'r', ' ', 'r', 'e', 'a', 'l'}
};
System.out.println(wordExistsInCharArray(puzzle1, "fighter"));
System.out.println(wordExistsInCharArray(puzzle2, "fighter"));
System.out.println(wordExistsInCharArray(puzzle3, "fighter"));
System.out.println(wordExistsInCharArray(puzzle4, "moth"));
System.out.println(wordExistsInCharArray(puzzle5, "moth"));
输出将是:
true
false
false
true
false