Pass Arraylist as argument to function

18

I have an arraylist A of Integer type. I created it as:

ArrayList<Integer> A = new ArrayList<Integer>();

Now, I want to pass it as an argument to function AnalyseArray().

How can I achieve this?

This question is tagged with java arraylist

~ Asked on 2013-06-15 15:38:16

The Best Answer is


27

public void AnalyseArray(ArrayList<Integer> array) {
  // Do something
}
...
ArrayList<Integer> A = new ArrayList<Integer>();
AnalyseArray(A);

~ Answered on 2013-06-15 15:40:55


5

The answer is already posted but note that this will pass the ArrayList by reference. So if you make any changes to the list in the function it will be affected to the original list also.

<access-modfier> <returnType> AnalyseArray(ArrayList<Integer> list)
{
//analyse the list
//return value
}

call it like this:

x=AnalyseArray(list);

or pass a copy of ArrayList:

x=AnalyseArray(list.clone());

~ Answered on 2013-06-15 15:45:27


Most Viewed Questions: