繁体   English   中英

如何检查两个字符串数组是否相同

[英]How to Check If Two String Arrays are The Same

我正在尝试解决“练习”上的问题,并且我无法通过所有测试。

问题是:

编写一个称为equals的方法,该方法接受两个字符串数组,如果相等则返回true;否则,返回true。 也就是说,如果两个数组的长度相同,并且在每个索引处包含等效的字符串值。

我已经尝试了以下代码,但是对输入的测试equals({"a", "b", "a", "c", "a", "d", "a", "e", "a"}, {"x", "b", "a", "c", "a", "d", "a", "e", "a"})但是它不起作用。

public static boolean equals (String [] txt1, String [] txt2){
    boolean result=false;
    if(txt1.length==txt2.length){
        for(int i=0; i<txt1.length; i++){
            if(txt1[i].equals(txt2[i])){
                result = true;
            }
            else {
                result = false;
            }
        }
    }
    else {
        return false;
    }
    return result;
}

预期收益: false我的收益: true

问题出在循环中:

for(int i=0; i<txt1.length; i++){
    if(txt1[i].equals(txt2[i])){
         result = true;
    }
    else {
         result = false;
    }
}

if对每个元素都执行了if ,那么从本质上讲,您的代码仅检查两个数组的最后一个元素是否相同,因为它会覆盖先前的result = false; 发生。

正确的解决方案是,一旦单个元素不同,立即停止并返回false

for(int i=0; i<txt1.length; i++){
    if(txt1[i].equals(txt2[i])){
         result = true;
    }
    else {
         return false;
    }
}

请参阅注释中的说明:

public static boolean equals(String[] txt1, String[] txt2) {
    if (txt1.length == txt2.length) {
        for (int i = 0; i < txt1.length; i++) {
            if (txt1[i].equals(txt2[i])) {
                // do nothing
            } else {
                // at this moment you know that arrays are different
                // so you can return false without checking the rest 
                // of the array
                return false;
            }
        }
        // here you checked all array, you know that each element is
        // same, because if it wouldn't, it would return false already
        // so you can return true now
        return true;
    } else {
        return false;
    }
}

可以对您的代码进行一些改进,再次阅读注释:

public static boolean equals(String[] txt1, String[] txt2) {
    if (txt1 == txt2) {
        // booth are null or booth are same instance
        return true;
    }
    if(txt1 == null || txt2 == null) {
        // one of the arrays is null so code bellow yould fail
        return false;
    }
    if (txt1.length == txt2.length) {
        for (int i = 0; i < txt1.length; i++) {
            if (!txt1[i].equals(txt2[i])){
                // equal rewriten to not equal
                return false;
            }
        }
        return true;
    }
    // no need to write else since every branch in if will result in return
    return false;
}

for循环之后for您应该检查数组是否不相等,如果相等,则返回false。 如果程序没有返回false,则返回true。

public static boolean equals(String[] txt1, String[] txt2) {
    if (txt1.length == txt2.length) {
        for(int i = 0; i < txt1.length; i++) {
            if(!txt1[i].equals(txt2[i])) {
               return true;
            }
        }
    } else {
        return false;
    }
    return true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM