【问题标题】:Compare two Arrays and find the index where the arrays dont match anymore in JAVA比较两个数组并在 JAVA 中找到数组不再匹配的索引
【发布时间】:2016-04-17 15:10:20
【问题描述】:

我有以下问题。 我需要实现一个方法来比较两个数组并找到给定数组的元素不匹配的第一个索引。 我已经尝试了以下代码,但它有点弱:

public int UnmatchedElementsOnIndex(int[] x, int[] y) {

    for (int i = 0; i < x.length; i++) {
        for (int j = 0; j < y.length; j++) {
            if (x[i] == y[j]) {
                // value is contained in both arrays
            }
            // otherwise not
        }
        return 0;
    }

【问题讨论】:

  • 这是错误的。您可以使用单个循环和一些 if 语句。
  • 如果找到不匹配的元素,我会使用一个额外的变量来计数 ++,但我不知道如何实现这一点
  • 先自己尝试,而不是从 SO 分数寻求者那里寻找答案:P。你可以做到。

标签: java arrays int


【解决方案1】:

您可以使用java.lang.Arrays.mistmatch 方法查找相同类型的两个数组之间第一个不匹配的索引。如果没有发现不匹配,则返回-1。

int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[] { 1, 2, 3, 4, 5, 3 };

        
Arrays.mismatch(a, b); // will return 5

此方法的性能将大大优于自定义代码,因为它以块的形式比较数组中的数据。

【讨论】:

    【解决方案2】:

    您只需扫描数组中的每个元素。然后确定哪个元素不匹配。以下是一种实现方式。

    public int UnmatchedElementsOnIndex(int[] x, int[] y) {
        int index=-1;
        //lengths should be equal. If not, we can't compare arrays
        if(x.length!=y.length){
            return -2;
        }   
        for (int i = 0; i < x.length; i++) {
            if (x[i] != y[i]) {
                index=i;
                break;
            }
        }
        //returns -1, if all the elements are equal
        return index;
    }
    

    【讨论】:

    • 这有帮助!谢谢
    【解决方案3】:

    试试这个:

    public int UnmatchedElementsOnIndex(int[] x, int[] y) {
    
            // Also check for the list length
            if(x.length < y.length)
              return x.length;
            else if(y.length < x.length)
              return y.length;
    
            for (int i = 0; i < x.length; i++) {
                  if(x[i] != y[i]) return i;
                }
                return -1;
            }
    

    如果两个数组中的所有元素都相同则返回-1,否则当数组在相同位置不包含相同元素时返回第一个索引。

    【讨论】:

    • 循环时可能导致 ArrayIndexOutOfBoundException。
    【解决方案4】:

    首先:有几种方法可以做到这一点。

    假设您想比较两个 int[],您可以像这样缩短代码:

        public static int getDifferIndex(int[] a, int[] b) {
            int i = 0;
            while (i < a.length && i < b.length && a[i] == b[i++]);
            return i;
        }
    

    在这里试试:https://ideone.com/zl0Hji

    假设您想要一个更通用的解决方案,该解决方案适用于实现 boolean equals(Object) 的对象,您可以将其重写为如下内容:

        public static int getDifferIndex(Object[] a, Object[] b) {
            int i = 0;
            while (i < a.length && i < b.length && a[i].equals(b[i++]));
            return i;
        }
    

    【讨论】:

      【解决方案5】:
          public class Main {
          public static void main(String args[]){
              int a[]={15,5,85,7};
              int b[] = {25,5,85,8};
              UnmatchedElementsOnIndex(a,b);
          }
          public static void UnmatchedElementsOnIndex(int[] x, int[] y) {
              int index=-1;
              int[] array = new int[x.length];
              int n=0;
              for (int i = 0; i < x.length; i++) {
                  if (x[i] != y[i]) {
                      array[n]=i;
                      n++;
                  }
              }
              for(int i=0;i<n;i++){
                  System.out.println(array[i]);
              }
      
          }
      }
      

      【讨论】:

        【解决方案6】:
        public int UnmatchedElementsOnIndex(int[] x, int[] y) {
            for (int i = 0; i < x.length && i < y.length; i++) {
                if (x[i] != y[i]) {
                    return i;
                }
                return -1;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-02
          • 2021-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多