Compare int[] with once Integer [duplicate]

守給你的承諾、 提交于 2019-12-13 08:35:08

问题


I have table of int[]. I have for loop with every user and I do:

int[] ids = (values) 
for (User user : listAfterProcessing) {
    if (user.getId().equals(ids)) { ... } 
}

They didnt work, Although the user id is in this table... Thanks for help :)


回答1:


You are checking whether the result of getId() equals the entire array ids, which is by default a comparison to the array's hashcode.

According to an answer in this question: How to convert int[] into List<Integer> in Java?

there is no quick way to convert.

The solution with Arrays.asList(), which I suggested in the first version of my answer, doesn't work here, sorry.




回答2:


Comparing an int array to a single Integer will not get you the result you want. You could go through the whole array and test every cell for the value, or you could keep the array sorted and do a binary search:

import java.util.Arrays;

Arrays.sort(ids);
if(Arrays.binarySearch(ids, user.getId().intValue()) >= 0) {}


来源:https://stackoverflow.com/questions/48242246/compare-int-with-once-integer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!