【问题标题】:How to simplify the code by using a loop?如何使用循环简化代码?
【发布时间】:2021-08-03 20:54:13
【问题描述】:

我应该找到一种方法来确定一个单词是否以对角线(从左上角到右下角)出现在二维数组中。 我的方法:static boolean findText (...) 在这种情况下有效,因为 LEGO 有 4 个字母,但当然这不是你想要的样子,它也应该能够工作更长时间和较短的单词。 我怎样才能做一个重复if (texts[k][i] == searchText[k]) 的循环,因为searchText 很长(所以基本上是searchText.length)?不会弄乱目前运行良好的另一个循环。

class FindText {
    public static void main(String[] args) {
        char[][] textField = {
                {'a', 'b', 'c', 'd', 'L', 'e', 'f', 'g', 'x'},
                {'h', 'i', 'j', 'k', 'l', 'E', 'm', 'n', 'a', 'b'},
                {'o', 'p', 'q', 'r', 's', 't', 'G', 'u'},
                {'v', 'w', 'x', 'y', 'z', 'a', 'b', 'O', 'y'},
                {},
                null};
        char[] searchText = {'L', 'E', 'G', 'O'};
        Out.print("\nText-Feld:\n--------------------------------\n");
        print(textField);
        Out.print("\n--------------------------------\n");
        print(searchText);
        if (findText(textField, searchText)) {
            Out.print("\nkommt vor!");
        } else {
            Out.print("\nkommt nicht vor!");
        }
    } // end main

    static boolean findText(char[][] texts, char[] searchText) {
        if (texts == null) return false;
        int k = 0;
        for (int i = 0; i < texts.length; i++) {
            if (texts[k][i] == searchText[k]) {
                if (texts[k + 1][i + 1] == searchText[k + 1]) {
                    if (texts[k + 2][i + 2] == searchText[k + 2]) {
                        if (texts[k + 3][i + 3] == searchText[k + 3]) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    } // end findText()

    static void print(char[][] texts) {
        if (texts == null) return;
        for (int i = 0; i < texts.length; i++) {
            if (texts[i] != null) print(texts[i]);
            Out.print("\n");
        }
    } // end printTexts()

    static void print(char[] text) {
        if (text == null) return;
        for (int i = 0; i < text.length; i++) {
            Out.print(text[i]);
        }
    } // end printText()
} // end class FindText

【问题讨论】:

    标签: java arrays loops multidimensional-array


    【解决方案1】:

    如果您的多维数组中没有 {null} 作为可能的条目,那么这将是一个简单的解决方案

    但是,因为我猜 null 是一个可能的条目,所以您需要一种方法来检查每个条目的值,然后再对其进行迭代。例如,texts[i].length 在达到 null 值时会崩溃。

    编辑:因为 {null} 是可能的条目,我刚刚在进入第二个循环之前添加了简单的 if 语句。我相信这应该适用于对角左上角搜索。

    编辑:必须在第三个循环的第一个 if 中添加另一个小的空检查。

    int size = 0;
    for (int i = 0; i < texts.length; i++) {
        if (texts[i] != null) {
            for (int j = 0; j < texts[i].length; j++) {
                // If the 1st letter matches
                if (texts[i][j] == searchText[0]) {
                    // then iterate over the size of the "searchtext"
                    for (int k = 1; k < searchText.length; k++) {
                        // If the position of the next letter exists & is equal to
                        // what is in the search word, then increment some variable
                        if ((i + k < texts.length &&
                                texts[i + k] != null &&
                                j + k < texts[i + k].length) &&
                                texts[i + k][j + k] == searchText[k]) {
                            size += 1;
                        }
                        // If we are on the last letter
                        if (k == searchText.length - 1) {
                            // return true if we have found the search word
                            if (size == searchText.length - 1) {
                                return true;
                            } else {
                                // Otherwise reset the size and
                                // keep iterating over the texts[][]
                                size = 0;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
    

    【讨论】:

    • 谢谢,它有效。我不会想到它会为此提供如此多的编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    相关资源
    最近更新 更多