如何搜索2D char数组,看看每行中是否有任何字符序列是java中的单词

时间:2015-10-27 21:06:45

标签: java arrays loops character

我有一个方法应该查看2D char数组的行,看看是否有任何字母序列是一个单词。例如,如果char数组的第0行是:{'b','o','x','e','n'} 该方法应打印出“盒子牛”。 但是,当我尝试遍历整个数组时,该方法仅显示数组最后一行中找到的字符。 有没有办法可以修复我的代码,以便它读取数组的所有行,而不仅仅是最后一行?

public static void locateWords(char [][] ch, String [] dictionary, int rowLength, int colLength, ArrayList<String> foundWords){
        StringBuilder rowChars = new StringBuilder(); //contains each row of characters in 2d array
            for(int i = 0; i<rowLength; i++){
                for (int start = 0; start < rowLength; start++){
                    for(int j = 1; j < colLength; j++){
                        rowChars.append(ch[start][j]);
                        if (checkWord(dictionary, rowChars.toString())) {
                           //checkWord() method checks to see if the characters form a word
                            foundWords.add(rowChars.toString());
                    }                  
                }
            }
            rowChars.delete(0, colLength); // clears out rowChars for next row
        }
    }

0 个答案:

没有答案