Kam*_*i81 0 java types contains biginteger
我从返回List of的数据库中提取数据值<Integer>.但是,我想看看List是否包含我的BigInteger.有一个简单的方法吗?
我目前在Java中有以下代码:
ArrayList<Integer> arr = new ArrayList<Integer>() {{add(new Integer(29415));}};
boolean contains = arr.contains(29415); // true
boolean contains2 = arr.contains(new BigInteger("29415")); // false
Run Code Online (Sandbox Code Playgroud)
我不确定有效的方法吗?
评估以下内容将返回正确答案:
val != null
&& BigInteger.valueOf(Integer.MIN_VALUE).compareTo(val) < 0
&& BigInteger.valueOf(Integer.MAX_VALUE).compareTo(val) > 0
&& list.contains(val.intValue())
Run Code Online (Sandbox Code Playgroud)
这将正确地解决BigInteger你是否"被包含"的问题List<Integer>.请注意,这里我们只在必要时向下转发.如果val超出Integer值范围,则无需向下转换,因为我们知道该值不能在列表中.
一个更相关的问题是你是否应该实际使用a List<BigInteger>代替a,List<Integer>但这是一个不同的问题而不是你明确问题的答案的一部分
| 归档时间: |
|
| 查看次数: |
799 次 |
| 最近记录: |