Java ArrayList to Array

Filed Under: Java

Sometimes we have to convert ArrayList to Array in java program. Today we will look into different java ArrayList to Array conversion options.

Java ArrayList to Array

java arraylist to array, arraylist to array java

ArrayList in Java is backed by Array. So it’s no wonder, ArrayList provides utility methods to convert ArrayList to Array. Let’s look at a simple program showing how to convert ArrayList to an array in java.


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

public class ArrayListToArray {

	public static void main(String[] args) {
		List<String> strList = new ArrayList<String>();
		strList.add("1");
		strList.add("2");
		strList.add("3");

		String[] strArr = null;

		strArr = strList.toArray(new String[strList.size()]);
		System.out.println(Arrays.toString(strArr));
	}
}

If you will look into ArrayList methods, toArray method is overloaded. The other one is without any argument that returns Object array. However, it’s not recommended because then you will have to cast the object to proper type for any operations.

ArrayList to Array in Java – other ways

While passing the array as argument in toArray method, we don’t need to provide the array with exact size. Below code works fine too.


strArr = strList.toArray(new String[0]);
System.out.println(Arrays.toString(strArr));

If you are using Java 8 or higher, you can use Stream too for converting ArrayList to Array.


strArr = strList.stream().toArray(String[]::new);
System.out.println(Arrays.toString(strArr));

Java ArrayList to Array – Shallow Copy

When we convert ArrayList to Array using above methods, it returns a shallow copy of the elements in the list. So any change in either ArrayList or Array will be reflected in other one too. Below is a simple program showing this behavior.


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

public class ArrayListToArrayMutableObjects {

	public static void main(String[] args) {
		Person p1 = new Person("Pankaj");
		Person p2 = new Person("Lisa");
		
		List<Person> pList = new ArrayList<>();
		pList.add(p1); pList.add(p2);
		
		Person[] pArray = pList.toArray(new Person[0]);
		
		System.out.println("Original List = "+pList);
		System.out.println("Created Array from ArrayList = "+Arrays.toString(pArray));
		
		//let's change the list and array
		pList.get(0).setName("David");
		pArray[1].setName("Ram");
		
		System.out.println("Modified List = "+pList);
		System.out.println("Modified Array = "+Arrays.toString(pArray));
		
	}

}

class Person {
	private String name;

	public Person(String n) {
		this.name = n;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return this.name;
	}
}

When we run above java program, it returns the following output.


Original List = [Pankaj, Lisa]
Created Array from ArrayList = [Pankaj, Lisa]
Modified List = [David, Ram]
Modified Array = [David, Ram]

As you can see that we changed the first element of ArrayList and second element of the Array. However, change is reflected in both ArrayList and Array and at the end, both have the same objects.

Java ArrayList to Array – Deep Copy

So if you want to convert ArrayList to Array and want them to be detached, then you will have to do a deep copy and write code for yourself. Below is a refactored code showing how to perform deep copy for converting ArrayList to Array.


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

public class ArrayListToArrayDeepCopy {

	public static void main(String[] args) {
		Person p1 = new Person("Pankaj");
		Person p2 = new Person("Lisa");
		
		List<Person> pList = new ArrayList<>();
		pList.add(p1); pList.add(p2);

		//convert ArrayList to Array using deep copy
		Person[] pArray = new Person[pList.size()];
		
		for(int i =0; i<pList.size(); i++) {
			Person p = pList.get(i);
			Person temp = new Person(p.getName());
			pArray[i] = temp;
		}
		
		System.out.println("Original List = "+pList);
		System.out.println("Created Array from ArrayList = "+Arrays.toString(pArray));
		
		//let's change the list and array
		pList.get(0).setName("David");
		pArray[1].setName("Ram");
		
		System.out.println("Modified List = "+pList);
		System.out.println("Modified Array = "+Arrays.toString(pArray));

	}

}

Below is the output produced by the above application.


Original List = [Pankaj, Lisa]
Created Array from ArrayList = [Pankaj, Lisa]
Modified List = [David, Lisa]
Modified Array = [Pankaj, Ram]

That’s all for java ArrayList to Array conversion using different methods.

Reference: Java ArrayList toArray API Doc

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages