Conversion Between Array and ArrayList in Java

Anna Scott
4 min readFeb 27, 2022

--

Array(fixed length data structure) VS ArrayList(variable length Collection class

Both array and ArrayList are important data structures in Java and frequently used in Java programs. Sometimes it is better to use one over the other, and we need to convert between these two structures.

Let’s first look into converting ArrayList into array. We will start by creating an ArrayList of integers 1 through 4. Since only objects could be stored in ArrayList, Java will use autoboxing with wrapper class Integer to turn our primitive integers into objects of class Integer.

Lets check that we have Integer objects in our ArrayList with operator instanceof.

The output is:

Class Integer

We will use method toArray() to convert our ArrayList into array. However we will run into a problem if we try to convert it into array of type int or even type Integer.

The above code will give us two errors:

incompatible types: Object[] can not be be converted to int[]

incompatible types: Object[] can not be converted to Integer[]

When method toArray() performs conversion ,it defaults to an array of type objects:

The output is: Class Object.

Notice that we were using out.println() instead of System .out.printl(). We were able to do it because of the static import as

In the following code snippet we specify type of array as Integer. We are also going to check the type of members of an array that we get after converting our ArrayList into array. When we specify the size of array as 0 java creates the array of needed size:

The output is:

Class Integer

The size of created array is 4

We can also specify different sizes. If our array bigger than ArrayList, than the array of specified size will be created and filled with Integer objects from ArrayList. If the space is left it would be filled with nulls. If we use the size smaller than our ArrayList java will create array of the size as ArrayList. Check out the following snippet:

We get the following output: 10

1

null

4

If we try to use toArray() method to create the array of primitives like so:

It won’t work, because if we take a look at the parameters that we are supposed to pass into toArray() we see that the type of array that we can pass is of type T.

In Java <T> stands for generic type. According to Java Docs — A generic type is a generic class or interface.

Thus we can not pass int[] to toArray() method.

Now lets take a look at converting an array to List. We will use Arrays.asList() method. We need to remember that our List can only contain objects, so we can not pass in array of integers like int[]. It has to be an array of a Wrapper class like Integer. In the following code snippet we are going to create an Integer type array and convert it into List:

The output is:

type of members is Integer: true

1

It is important to notice that the created array is has fixed size list. The following line:

produces UnsupportedOperationException.

Now take a look at the following snippet and its output:

The output is: 10

Both — our array and list are connected by pointing to the same data, and when we change one the other one also gets updated.

We can see that Arrays.asList() and Collection.toArray() methods allow to connect array based and collection based Java APIs.

Happy coding my friends!

All code can be found here

--

--