Skip to Content

How to pass ArrayList as array in Java?

Arrays and ArrayLists are two of the most commonly used data structures in Java. While arrays have a fixed size, ArrayLists are dynamic and can grow and shrink in size. Sometimes we need to convert between these two data structures. One common requirement is to pass an ArrayList to a method that expects an array. In this article, we will explore different options to achieve this.

Why pass ArrayList as array?

There are a couple of reasons why you may need to pass an ArrayList as an array in Java:

  • The method you want to call expects an array, not an ArrayList. For example, you want to sort an ArrayList using Arrays.sort().
  • You want to pass a snapshot of the ArrayList instead of passing the ArrayList itself. This avoids changes in the original ArrayList affecting the array.
  • The code expects primitive arrays, while ArrayList contains wrapper objects like Integer.

In these cases, you need to convert the ArrayList to an array before passing it to the method.

Method 1: Using toArray()

The simplest way to convert an ArrayList to an array is by using the toArray() method of the ArrayList class. For example:


import java.util.ArrayList; 
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {

    ArrayList<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    
    Integer[] array = list.toArray(new Integer[0]);
    
    System.out.println(Arrays.toString(array));
  }

}

Here we create an ArrayList of Integers, populate it with some values and then convert it to an array using toArray(). We pass 0 as the argument to specify the returned array should be a new array of the same size as the ArrayList. The output will be:

[1, 2, 3]

If we did not pass the new Integer[0], it would have returned the default Object[] instead of Integer[].

This works well in most cases. One thing to watch out for is that toArray() returns the ArrayList elements themselves, not a copy. So changes made to the returned array will be reflected in the original ArrayList as well. If you want to avoid this, create a copy of the array:


Integer[] array = list.toArray(new Integer[0]); 
array = Arrays.copyOf(array, array.length);

Method 2: Using ArrayList constructor

We can also use one of the ArrayList constructors that takes an array to convert an array to an ArrayList:


Integer[] array = {1, 2, 3};
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array)); 

Here we create an Integer array and pass it to Arrays.asList() to get a fixed size List from the array. We then pass this List to the ArrayList constructor to create the ArrayList.

One caveat of this approach is that the ArrayList is backed by the original array. So changes to either the ArrayList or array will be reflected in both. If you want to avoid this, create a copy of the ArrayList:


Integer[] array = {1, 2, 3};
List<Integer> list = Arrays.asList(array);
ArrayList<Integer> copy = new ArrayList<>(list); 

Method 3: Manual copying

We can also manually copy elements from the ArrayList to an array:


ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);

Integer[] array = new Integer[list.size()];
for(int i = 0; i < list.size(); i++) {
  array[i] = list.get(i); 
}

Here we first create an array of the same size as the ArrayList. Then we loop through the ArrayList and copy each element to the array using get().

This avoids any backreferences between the ArrayList and array. But it involves manual copying which can get tedious for large lists.

Method 4: Using streams

Java 8 introduced streams which provide a neat way to convert between collections. We can convert an ArrayList to an array using streams:

  
ArrayList<Integer> list = new ArrayList<>();
list.add(1); 
list.add(2);
list.add(3);

Integer[] array = list.stream().toArray(Integer[]::new); 

toArray() on the stream automatically copies the ArrayList elements to a new array. Streams provide several other useful methods like mapping, filtering, etc. while copying elements.

Method 5: Using Apache Commons Lang

The Apache Commons Lang library provides utility methods for working with Java collections. We can use its ArrayUtils class to convert between Lists and arrays:


import org.apache.commons.lang3.ArrayUtils;

ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);  
list.add(3);

Integer[] array = ArrayUtils.toArray(list); 

ArrayUtils.toArray() copies the ArrayList to a new array so there are no references between the two. Apache Commons Lang provides several other useful utilities as well.

Passing ArrayList to varargs method

Sometimes you need to pass an ArrayList to a varargs method like:


public void varargsMethod(Integer... nums) {
} 

Since varargs are backed by arrays, we can directly pass our ArrayList after converting it:


ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);

varargsMethod(list.toArray(new Integer[0]));

This will call the varargs method passing the ArrayList elements as separate arguments.

Primitive arrays

All the above methods work with reference type arrays like Integer[]. But sometimes you may need a primitive int[] instead of Integer[].

In this case, you need to convert the Integer objects to primitive ints while copying to the array:


ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);

int[] primArray = new int[list.size()];
for(int i = 0; i < list.size(); i++) {
  primArray[i] = list.get(i); //autoboxing
}

Or with streams:


int[] primArray = list.stream()
                    .mapToInt(i -> i)
                    .toArray();

This performs autoboxing while converting Integer to primitive int.

Passing sublists as arrays

Sometimes you may want to pass a portion of an ArrayList as an array. Here is how you can do it:


ArrayList<Integer> list = new ArrayList<>();
//add elements

int fromIndex = 1;
int toIndex = 3;

Integer[] subarray = list.subList(fromIndex, toIndex)
                          .toArray(new Integer[0]);
  

We use ArrayList.subList() to get the range of elements we want and convert that to an array. subList() returns a view of a portion of the original list without copying elements.

Conclusion

We looked at several ways to convert an ArrayList to an array in Java:

  • Using ArrayList.toArray()
  • Passing the ArrayList to Arrays.asList() and ArrayList constructor
  • Manually copying elements
  • Using Java streams
  • Apache Commons Lang ArrayUtils

toArray() is the simplest option in most cases. Streams and Apache Commons Lang provide cleaner and more readable options. Manual copying should be used only when you want to avoid any references between ArrayList and array.

When converting to primitive arrays, take care to handle autoboxing and unboxing. Passing sublists as arrays can be achieved using ArrayList.subList() and toArray().

So in summary, Java provides multiple ways to easily convert between ArrayLists and arrays to suit different needs.