简体   繁体   English

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

[英]how to compare two string arrays without java utils

Check to see if the array arr1 contain the same elements as arr2 in the same order in java. 检查数组arr1是否在java中以相同的顺序包含与arr2相同的元素。

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. 这个问题是它只比较两个数组的第一个元素。

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 equals not == 你应该寻找一个不匹配的String,你应该使用equals not ==

// 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 null values as well. FYI这是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;
}
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. 但这是非常不理想的。

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 will bite you someday for real data. 虽然由于字符串实习而使用==不是所提供的示例数据的问题,但有一天它咬你的真实数据。 Don't use == for objects unless identity equality is desired. 除非需要标识相同 ,否则不要对对象使用==

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

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