如何在Java中逐元素比较两个数组? [英] How to compare two arrays element by element in Java?

查看:68
本文介绍了如何在Java中逐元素比较两个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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. 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!

推荐答案

您可以检查索引是否小于两个数组的大小.

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()) {
             }
     }

这篇关于如何在Java中逐元素比较两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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