CodeGym /Java Blog /Java Collections /Conversion of Array To ArrayList in Java
Author
Artem Divertitto
Senior Android Developer at United Tech

Conversion of Array To ArrayList in Java

Published in the Java Collections group
Hi! In today's lesson, we'll talk about How To Initialize An Array In Java and How Convert Array to an Arraylist. Arrays are an extension of the Container class in Java and can be useful as a way to temporarily hold a set of primitive data types when the quantity of data is specifically known. But Arrays are static when created, which means that when you create one, you cannot change the number of items it holds. You should remember how to initialize an array in java; it looks like this:

datatype[] isArray;
Where datatype is any primitive data type like int, float, long, or even String. You can also place the brackets after the declaration like so:

datatype isArray[];
So how do we take this static array with a set number of elements and convert it to an ArrayList? Well, first, let’s look at what an ArrayList is.

The ArrayList

An ArrayList may have a similar name to an Array but it is handled completely differently. That’s because an ArrayList doesn’t extend the Container class, it extends the List class. And that means that everything gets handled differently. For one thing, because it is a List, you can manipulate it differently. You can increase or decrease the size of the ArrayList using add(element), which places the element at the end of the List and increases the size by one if necessary. Or you can use trimToSize() which removes any empty indices at the end and trims the ArrayList to its current size. So you can see that there are a few advantages to using an ArrayList instead of just an array. Next we’re going to show you two methods to convert from an Array to an ArrayList and how to move back if you need to.

Moving from Array to ArrayList and ArrayList to Array in Java

So, let’s say that you have written a program to keep an inventory of how many nuts and bolts your company has on hand for repairing cat trees. For years, you have only needed 30 different types of them, so it’s been easy to just use an array to keep track. But now you have a new client who requires you to stock an additional 5 types. How can you change your program and keep all of your data, AND prevent yourself from having to do this again when you pick up another client? That’s right! An ArrayList! So how do you convert a Java Array to an ArrayList? There are three methods. Using the .asList() method Java arrays have a great tool that you can see when you look at the API called .asList(). So you could just write in this:

boltInventory.asList(bolts);
The problem with this method is that it doesn’t create a true ArrayList. What it does is create a List that is fixed in size and unchangeable. So you still can’t change the size in a dynamic way. Trying to add or remove elements will cause an exception. So while this does have its uses, it isn’t a true conversion. But we can use this. Using the .asList() as a an Argument This is the second way to convert an array to list in Java. Because the .asList() method creates a List, we can use that to pass a reference to our actual ArrayList. Here is how to initialize an ArrayList:

ArrayList<Integer> boltsInventory = new ArrayList<Integer>();
This will create an ArrayList that has ten empty cells. However, the () at the end can be used to pass an argument to fill the ArrayList. So combining with the .asList method, you have:

ArrayList<Integer> boltsInventory = new ArrayList<Integer>(Arrays.asList(bolts));
This passes the List created by the .asList() method into the ArrayList, so now you can dynamically manipulate it as you need. Using the Collections.addAll() method Another way to convert an Array to an ArrayList in Java is to use this method. It passes the contents of the Array to the ArrayList. The generic syntax for this method is:

Collections.addAll(c, T);
Where c is the destination and T is what is being passed. So for our example, the syntax would look like this:

ArrayList<Integer> boltsInventory = new ArrayList<Integer>():
Collections.addAll(boltsInventory, bolts);
This will pass the entire contents of the Array bolts to the new ArrayList. Converting an ArrayList to an Array There may be a time when you need to convert an ArrayList to an Array in Java. If you do, the ArrayList has a method .toArray(a), where a is the destination. So for our example, the syntax would be:

Integer boltsInventoryArray[] = new Integer{boltsInventory.size()];
// this ensures the newly created array is of the same size as the ArrayList
boltsInventoryArray = boltsInventory.toArray(boltsInventoryArray);
When you change a list to array in Java like this, you are creating a deep copy. That is, all references to the Array are different from the references to the ArrayList. So you can manipulate the data in the Array without changing the data stored in the ArrayList. It is useful to convert a java list to an array when you need to test data.

Conclusion

Arrays are very useful for fast access and quick manipulation of smaller data sets, but the inability to change their size mean that they quickly lose efficiency for long term use. An ArrayList gives you that flexibility and the ability to insert and remove nodes as needed. Learning how to convert a Java Array to a List will improve the overall efficiency of your programs and improve their runtimes as well.
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jeongmin Level 8, Tsche-mul-p-ho, Korea, Republic of
12 September 2022
The code "Integer boltsInventoryArray[] = new Integer{boltsInventory.size()];" needs to be changed to "Integer boltsInventoryArray[] = new Integer[boltsInventory.size()];"