[英]issue with Arrays.asList()
I have a very simple program and I just need to check an array for a value in it. 我有一个非常简单的程序,我只需要检查数组中的值。 I have a class called bulkBean.
我有一个名为bulkBean的类。 this is it.
就是这个。
public class bulkBean {
private int installmentNo;
private double amount;
public int getInstallmentNo() {
return installmentNo;
}
public void setInstallmentNo(int installmentNo) {
this.installmentNo = installmentNo;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
Now I have an array of this bulkBean type in my program, this is my program. 现在我的程序中有一个这个bulkBean类型的数组,这是我的程序。
import java.util.Arrays;
public class test {
public static boolean scan_bulkList(bulkBean[] bulkList, int i) {
int[] arr = new int[bulkList.length];
for(int x=0;x<bulkList.length;x++){
arr[x] = bulkList[x].getInstallmentNo();
}
for(int j = 0; j< arr.length ;j++){
System.out.println("INFO: array "+j+" = "+arr[j]);
}
if (Arrays.asList(arr).contains(i) == true) {
return true;
} else {
return false;
}
}
public static void main(String[] arg){
bulkBean bb1 = new bulkBean();
bb1.setInstallmentNo(1);
bb1.setAmount(5500);
bulkBean bb2 = new bulkBean();
bb2.setInstallmentNo(2);
bb2.setAmount(4520);
bulkBean[] bulkArray = new bulkBean[2];
bulkArray[0] = bb1;
bulkArray[1] = bb2;
boolean a = scan_bulkList(bulkArray,1);
System.out.println("val = "+a);
}
}
I create 2 instances of bulk bean and I set values to them. 我创建了2个批量bean实例,并为它们设置了值。 Then I added those two instances to an array.
然后我将这两个实例添加到一个数组中。 Then I pass that array to the method to check for a value(also given as a parameter. In this case it is 1.).
然后我将该数组传递给方法以检查值(也作为参数给出。在这种情况下,它是1.)。 If the array contains that value, it should return true, otherwise false.
如果数组包含该值,则应返回true,否则返回false。 whatever value I enter, it return false.
无论我输入什么价值,它都会返回false。 Why do I get this issue?
为什么我会遇到这个问题?
Arrays.asList()
returns a List which has a single element - an array. Arrays.asList()
返回一个具有单个元素的List - 一个数组。 So, you are actually comparing against an array . 所以,你实际上是在与数组进行比较。 You need to compare against each value in the array.
您需要与数组中的每个值进行比较。
As TheListMind told, Arrays.asList()
taken on an int[]
gives you a list containing the array. 正如TheListMind所说,在
int[]
上采用的Arrays.asList()
为您提供了一个包含该数组的列表。
Personally, I would construct directly the List instead of constructing the array, or even better (no need of array instanciation), test while iterating the bulk array : 就个人而言,我会直接构造List而不是构造数组,甚至更好(不需要数组实例化),在迭代批量数组时进行测试:
for(int x=0;x<bulkList.length;x++){
if (bulkList[x].getInstallmentNo() == i){
return true;
}
}
return false;
The mistake you made here is , you created the int array which must be Integer array because Arrays.asList().contains(Object o);
你在这里犯的错误是,你创建了int数组,它必须是Integer数组,因为
Arrays.asList().contains(Object o);
makes the input parameter also Integer(Integer i)
. 使输入参数也为
Integer(Integer i)
。 int
is not an object Integer
is the object. int
不是对象Integer
是对象。 Hope it will work. 希望它会奏效。
int[] arr = new int[bulkList.length];
change to: 改成:
Integer[] arr = new Integer[bulkList.length];
Change the method as below to avoid complications: 更改方法如下,以避免并发症:
public static boolean scan_bulkList(bulkBean[] bulkList, int i) {
int[] arr = new int[bulkList.length];
for(int x=0;x<bulkList.length;x++){
arr[x] = bulkList[x].getInstallmentNo();
if (bulkList[x].getInstallmentNo()==i) {
return true;
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.