user5767766
user5767766

Reputation:

How to create ArrayList without using add-function

As the title implies, I'd like to know how to insert different values into my ArrayList, without using too much space for several "add" functions.

    ArrayList<Integer> arr = new ArrayList<Integer>(Arrays.asList(3,4));
    ArrayCheck.allDivisibleBy(arr, divisor);

I have got an arraylist called arr, and I don't know if this is the right way to add several values (3,4) that way.

Furthermore I would also like to check the values in another method called allDivisbleBy. The function of this method is not relevant though, but I want to check the values and am not sure if "ArrayCheck" is a way to send the array values to the method.

Upvotes: -1

Views: 12541

Answers (3)

granmirupa
granmirupa

Reputation: 2790

I don't know if this is the right way to add several values (3,4) that way

Yes it is. Else you can use this:

ArrayList<Integer> arr = new ArrayList<Integer>(2);

arr.add(3);
arr.add(4);

I want to check the values and am not sure if "ArrayCheck" is a way to send the array values to the method

ArrayCheck is not a standard java class.

Upvotes: 0

Alain Van Hout
Alain Van Hout

Reputation: 480

The simplest way is to use Arrays.asList(arr). As expected, that static method returns a List with as it's contents the elements of the array.

Upvotes: 1

Alireza Mohamadi
Alireza Mohamadi

Reputation: 521

Check all of your inputs for your condition, put them in a Collection and use method addAll instead of add to add all of collection items at once.

Upvotes: 0

Related Questions