java 如何在没有java utils的情况下比较两个字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14127173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:10:21  来源:igfitidea点击:

how to compare two string arrays without java utils

javaarrayscompare

提问by user1927368

Check to see if the array arr1 contain the same elements as arr2 in the same order in java.

检查数组 arr1 是否包含与 arr2 相同的元素,在 java 中的顺序相同。

for example:

例如:

    isTheSame({"1", "2", "3"}, {"1", "2", "3"}) → true
    isTheSame({"1", "2", "3"}, {"2", "1", "1"}) → false
    isTheSame({"1", "2", "3"}, {"3", "1", "2"}) → false

so far i have

到目前为止我有

public boolean isTheSame(String[] arr1, String[] arr2)
{
   if (arr1.length == arr2.length)
   {
      for (int i = 0; i < arr1.length; i++)
       {
          if (arr1[i] == arr2[i])
          {
            return true;
          }
       }
    }
    return false;  
 }

The problem with this is that it only compares the first element of the two arrays.

问题在于它只比较两个数组的第一个元素。

回答by Peter Lawrey

You are iterating until you find a match. You should instead be looking for a String which doesn't match and you should be using equalsnot ==

您正在迭代,直到找到匹配项。你应该寻找一个不匹配的字符串,你应该使用equalsnot==

// same as Arrays.equals()
public boolean isTheSame(String[] arr1, String[] arr2) {
    if (arr1.length != arr2.length) return false;
    for (int i = 0; i < arr1.length; i++)
        if (!arr1[i].equals(arr2[i]))
            return false;
    return true;
}

FYI This is what Arrays.equals does as it handle nullvalues as well.

仅供参考 这就是 Arrays.equals 所做的,因为它也处理null值。

public static boolean equals(Object[] a, Object[] a2) {
    if (a==a2)
        return true;
    if (a==null || a2==null)
        return false;

    int length = a.length;
    if (a2.length != length)
        return false;

    for (int i=0; i<length; i++) {
        Object o1 = a[i];
        Object o2 = a2[i];
        if (!(o1==null ? o2==null : o1.equals(o2)))
            return false;
    }

    return true;
}

回答by Kirill Lebedev

public boolean isTheSame(String[] arr1, String[] arr2)
{
    if (arr1.length == arr2.length)
    {
         for (int i = 0; i < arr1.length; i++)
          {
             if ((arr1[i] != null && arr2[i] != null && !arr1[i].equals(arr2[i]))
                 || (arr1[i] != null && arr2[i] == null) || 
                 (arr2[i] != null && arr1[i] == null))
              {
                return false;
              }
          }
    } else {
         return false;  
    }
    return true;  
 }

But it is very unoptimal.

但这是非常不理想的。

回答by Kirill Lebedev

Yes, it will only compare the first element. Look what it is doing:

是的,它只会比较第一个元素。看看它在做什么:

public boolean isTheSame(String[] arr1, String[] arr2)
{
    if (arr1.length == arr2.length)
    {
         for (int i = 0; i < arr1.length; i++)
          {
             // if the elements are equal ..
             // (NOTE: this will only be true for INTERNED strings!!)
             if (arr1[i] == arr2[i]) // fixed typo
              {
                // .. then STOP LOOKING and RETURN TRUE
                // wait- WHY?
                return true;
              }
          }
          // searched everything- but still RETURN FALSE??
    }
    return false;  
 }

While the usage of ==is not the problem with the provided example data due to string interning, it willbite you someday for real data. Don't use ==for objects unless identity equalityis desired.

虽然==由于字符串实习,所提供的示例数据的使用不是问题,但总有一天它咬你以获得真实数据。==除非需要身份相等,否则不要用于对象。