比较Java中的数组以及索引位置 [英] comparing arrays in java along with index position

查看:63
本文介绍了比较Java中的数组以及索引位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一次Java采访中,有人问我如何编码数组元素,以比较多少个元素或索引相等以及多少个元素不相等?有没有一种方法可以在不使用for或while循环的情况下比较Java中的数组值?我想到的第一件事是Arrays类为我们提供了实用方法 equals() deepEquals(),但是最后我无法弥补,请告知举一个小例子,我该如何解决这个问题?

In an java interview I was asked that how would you code to compare Array elements with each other to find out how many elements or indexes are equals and How many elements are not equal? Is there a way I can compare Array values in Java without using a for or while loop? The first thing that came in my mind that Arrays class provides us utility methods equals() and deepEquals() , but at last I was not able to make up, please advise with a small example that how can I solve this problem?

 int[] i1 = new int[] {1,2,3,4};
 int[] i2 = new int[] {0,5,3,3};

推荐答案

类似

notEqualCount += Math.abs(i1.length - i2.length);
for(int i=0; i<i1.length && i < i2.length;i++){
  if(i1[i]==i2[i]){
    equalCount++;   
  } 
  else{
    notEqualCount++; 
  }
}

这篇关于比较Java中的数组以及索引位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆