-6

Is there a way to pass an ArrayList to a method as an argument? Here's what I've got so far that isn't working:

int[] exampleMethod(ArrayList<int> exampleList) {

}

Any help is greatly appreciated!

Mr. Lavalamp
  • 1,860
  • 4
  • 17
  • 29
  • Yes it is correct, what is the problem? – Juned Ahsan Jul 17 '13 at 05:36
  • 7
    Look all the people who tries to answer a duplicated question instead of voting to close (especially those with more than 3k rep). – Luiggi Mendoza Jul 17 '13 at 05:36
  • 2
    I searched for it and couldn't find it, so obviously it's worth having another question about it then isn't it? Someone could easily be searching for it using my wording rather than yours, and this could help them. – Mr. Lavalamp Jul 17 '13 at 05:40
  • No it's almost correct.Your ArrayList type should be Integer not int as the primitive.Besides that your wrapper is okay – alvinarul Jul 17 '13 at 05:41
  • @Mr.Lavalamp it would have been nice to also post the compilation errors you were getting – aldrin Jul 17 '13 at 05:42
  • *I searched for it and couldn't find it, so obviously it's worth having another question about it then isn't it?* I just wrote `arraylist` on the net and this link appeared: [How to declare an ArrayList that can hold int variables](http://www.java-forums.org/new-java/57349-how-declare-arraylist-can-hold-int-variables.html) – Luiggi Mendoza Jul 17 '13 at 05:43
  • @LuiggiMendoza Well is the rule not to keep things that can be found easily already on SO or on the entire internet? By your logic we should just throw half the site out. – Mr. Lavalamp Jul 17 '13 at 05:58
  • Or do more research before posting a question. – Luiggi Mendoza Jul 17 '13 at 05:59

5 Answers5

2

Replace int with Integer then it will surly work :)
So it would be

int[] exampleMethod(ArrayList<Integer> exampleList) {

}

Keep in mind that collection are always expecting wrapper class instead of primitive datatypes.
int is primitive datatype and Integer is its wrapper class.

Freak
  • 6,786
  • 5
  • 36
  • 54
1

Yss.

With Wrapper classes.

int[] exampleMethod(ArrayList<Integer> exampleList) {

}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Use the Wrapper class :

int[] exampleMethod(ArrayList<int> exampleList) {

}

List<E> cannot take primitive datatypes like int , hence we are provided with their wrapper types like Integer. You can also pass the List<Integer> interface type rather than the implementation type for re usability.

Read Oracle tutorials here .

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

Try this

int[] exampleMethod(ArrayList<Integer> exampleList) {


    }
subodh
  • 6,136
  • 12
  • 51
  • 73
1

As already has been noted, you must use the wrapper class

 int[] foo(List<Integer> oArray)
 {
 }

int is a primitive type, so you can not use it in a List. This also holds for other primitive types like char, boolean, float etc. In all these cases, when you use them where an object is required, you must use their class counterparts (for example in a Map).

As you can see in my example, you should accept a List<Integer> in your method, instead of ArrayList. ArrayList is a specialized class, so when you limit it to ArrayList you can only accept this typer, while if you use the interface typem, you can accept any type of List and your code should still work.

Devolus
  • 21,661
  • 13
  • 66
  • 113