问题
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