0

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.

granmirupa
  • 2,780
  • 16
  • 27
  • 2
    So what is your exact Question? Or you basically want us to write your whole code? Regarding `Arrays.asList` see http://stackoverflow.com/questions/20538869/what-is-the-best-way-of-using-arrays-aslist-to-initialize-a-list – Mad Matts May 07 '16 at 16:28
  • 1. Yeah, you *can* create an ArrayList that way. It's not actually any more efficient than adding these values, though. In fact, it's less efficient. 2. ArrayCheck isn't a class that I've ever heard of, or one that I can find in the documentation. – Maura May 07 '16 at 16:29
  • 4
    You can probably just do `List arr = Arrays.asList(3, 4);` and get the same functionality. It sounds like you don't need the List to be modifiable. – David Ehrmann May 07 '16 at 16:30
  • The way you currently have is not using many add methods. I'm confused what the problem is – OneCricketeer May 07 '16 at 16:30
  • Okay, I think I have not been clear enough, sorry. I am working with jUnit. I have got a method in another test.java where I test the method in my mainclass. In the test method I just want to call the function in my mainclass to check the following values (but that function is not important for the question). I just want to create an ArrayList as in the code, and lateron call another function with the values I inserted in the array, and I dont know how. I have read something about ArrayCheck, but that does not work. –  May 07 '16 at 16:34
  • Please provide some link to where you see ArrayCheck – OneCricketeer May 07 '16 at 16:46

3 Answers3

1

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.

Alain Van Hout
  • 409
  • 3
  • 7
0

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.

granmirupa
  • 2,780
  • 16
  • 27
0

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.

Alireza Mohamadi
  • 511
  • 5
  • 19