简体   繁体   English

如何在Java中逐元素地比较两个数组?

[英]How to compare two arrays element by element in Java?

I have two arrays of objects in Java, which contain some fields that I need to compare, but the thing is I need to compare element by element, that means, that I want the compare a field from the first object in my first array with the first object in my second array, my second object from the first array with my second object from the second array and so on. 我在Java中有两个对象数组,其中包含一些我需要比较的字段,但问题是我需要逐个元素地比较,这意味着,我希望比较第一个数组中第一个对象的字段第二个数组中的第一个对象,第一个数组中的第二个对象,第二个数组中的第二个对象,依此类推。 This is what I have done so far, but the idea is that I do not know what should be the limit for my second array. 这是我到目前为止所做的,但我的想法是我不知道我的第二个数组应该是什么限制。 From my point of view the second array should start from the index of the first array like this: 从我的角度来看,第二个数组应该从第一个数组的索引开始,如下所示:

for(int i = 0; i < resultEntries.size(); i++) {
    for(int j = i; j < resultColorEntries.size(); j++) {
        if(resultEntries.get(i).getColor())...
    };
}

Another solution or a solution to my problem would be welcome. 我的问题的另一个解决方案或解决方案将受到欢迎。 Thanks in advance! 提前致谢!

如果你想更有效地与O(N)进行比较,你需要实现你的对象hash(),并将你的项添加到一个hashset,例如arrayA的arraySet和arrayB的hashsetB,然后比较这两个hashsets。

You don't have to make 2 loops, you can achieve this only with one: 您不必进行2次循环,只能使用一个循环:

for(int i = 0; i < resultEntries.size() && i < resultColorEntries.size(); i++) {
    if(resultEntries.get(i).getColor().equals(resultColorEntries.get(i).getColor()) {
         // Same!
    }
}

that I want the compare a field from the first object in my first array with the first object in my second array, my second object from the first array with my second object from the second array and so on 我希望比较第一个数组中第一个对象与第二个数组中第一个对象的字段,第一个数组中的第二个对象和第二个数组中的第二个对象,依此类推

You shouldn't have to write nested loop then. 您不应该编写嵌套循环。 One loops will do. 一个循环就行了。

Codes should be 代码应该是

for(int i=0;i<resultEntries.size();i++){

 if(resultEntries.get(i).getColor().equals(resultColorEntries.get(i).getColor()) )...

Assuming you have the same size of lists. 假设您有相同大小的列表。

You can keep check of index that it is less than the size of both arrays. 您可以检查索引是否小于两个数组的大小。

for(int i=0;i<resultEntries.size() && i<resultColorEntries.size();i++){
             if(resultEntries.get(i).getColor()) {
             }
     }

The whole thing only makes sense when both arrays have equal dimensions; 当两个阵列具有相同的尺寸时,整个事情才有意义; so you could go for: 所以你可以去:

Whatever a[] = ...
Somthinelse b[] = ...

check for equal length

for (int i=0; i < a.length; i++) {
  if (a[i].whatever.equals(b[i].whatever))...

Point is: no need for two loops! 重点是:不需要两个循环!

But: there is no nice way of generalizing that (like: writing a method that compares those arrays; but on different fields for example). 但是:没有很好的推广方法(比如:编写一个比较这些数组的方法;但是在不同的字段上)。 Well, you could use method references, and using method references, ending up with something like 好吧,你可以使用方法引用,并使用方法引用,最终得到类似的东西

public void compareArraysOn(Whatever[] a, Other[] b, Function<T,U> extractorForA, ...) {

that could be called like compareArraysOn(a, b, Whatever::getColor(), SomethinElse::getColor()) 可以像compareArraysOn(a, b, Whatever::getColor(), SomethinElse::getColor())一样调用

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

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