简体   繁体   中英

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. 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.

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. 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.). If the array contains that value, it should return true, otherwise false. whatever value I enter, it return false. Why do I get this issue?

Arrays.asList() returns a List which has a single element - an array. 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.

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 :

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); makes the input parameter also Integer(Integer i) . int is not an object Integer is the object. 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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM