In this post we’ll see how to convert ArrayList to array in Java.
Since internal implementation of ArrayList uses an array internally to store its element so ArrayList class itself provides toArray()
method to convert ArrayList to array. There are two overloaded toArray() methods.
- toArray()- Returns an array containing all of the elements in this list in proper sequence. The returned array will be "safe" in that no references to it are maintained by this list. Returned array is of type Object.
- toArray(T[] a)- Returns an array containing all of the elements in this list in proper sequence. The runtime type of the returned array is that of the specified array.
Though one problem with using these methods to convert ArrayList to array in Java is that the reference of the stored objects are shared between both ArrayList and converted array. Any modification to any stored object will be reflected in other data structure too.
Another option is to do the conversion manually without using any method. See example.
First we'll see examples of using toArray() methods to convert ArrayList to array. We'll also modify the stored object to show that the modification is reflected in the other data structure too. For that we’ll have to store custom objects in ArrayList so here is a class whose objects are stored in ArrayList.
public class Employee{ private int empId; private String empName; private int age; Employee(int empId, String empName, int age){ this.empId = empId; this.empName = empName; this.age = age; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return getEmpId() + " " + getEmpName() + " " + getAge(); } }
Using toArray(T[] a) method to convert ArrayList to array
Here we have an ArrayList storing objects of type Employee. Then this ArrayList is converted to array. Then an object in ArrayList is modified, same way an object is modified in array. As you can see both the changes are reflected in both ArrayList as well as array.
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListToArray { public static void main(String[] args) { List<Employee> empList = new ArrayList<Employee>(); empList.add(new Employee(1, "Zhiang", 34)); empList.add(new Employee(2, "Marie", 23)); empList.add(new Employee(3, "Amy", 31)); empList.add(new Employee(4, "Robbie", 45)); // creating array of same size as list Employee[] empArr = new Employee[empList.size()]; // Converting ArrayList to array empArr = empList.toArray(empArr); System.out.println("Employees- " + Arrays.toString(empArr)); // Making change in list element empList.get(3).setEmpName("Jack"); // Making change in Array element empArr[1].setAge(45); System.out.println("After modifications"); System.out.println("List elements-- " + empList); System.out.println("Employees- " + Arrays.toString(empArr)); } }Output
Employees- [1 Zhiang 34, 2 Marie 23, 3 Amy 31, 4 Robbie 45] After modifications List elements-- [1 Zhiang 34, 2 Marie 45, 3 Amy 31, 4 Jack 45] Employees- [1 Zhiang 34, 2 Marie 45, 3 Amy 31, 4 Jack 45]
Using toArray() method to convert ArrayList to array
import java.util.ArrayList; import java.util.List; public class ListToArray { public static void main(String[] args) { List<String> carList = new ArrayList<String>(); carList.add("Audi"); carList.add("Jaguar"); carList.add("BMW"); carList.add("Mini Cooper"); Object[] carArr = carList.toArray(); for(Object car : carArr) { System.out.println("Car- " + car); } } }
As you can see the converted array is of type Object which is a drawback of using this method.
Converting ArrayList to array without any API methods
You can also convert ArrayList to array in Java by iterating the ArrayList and then assigning elements to array.
public class ListToArray { public static void main(String[] args) { List<String> carList = new ArrayList<String>(); carList.add("Audi"); carList.add("Jaguar"); carList.add("BMW"); carList.add("Mini Cooper"); // creating array of same size as list String[] carArr = new String[carList.size()]; // iterate list and assign values to array for(int i =0; i < carList.size(); i++){ carArr[i] = carList.get(i); } System.out.println("Cars- " + Arrays.toString(carArr)); } }
That's all for the topic Convert ArrayList to Array in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
- How to Sort ArrayList of Objects in Java
- Java Program to Remove Duplicate Elements From an Array
- Java Volatile Keyword With Examples
- Java StringBuilder With Method Examples
- Java Program to Check if The Given Strings Are Anagram or Not
- Spring Autowiring Example Using XML Configuration
- Fix LazyInitializationException: could not initialize proxy Error
- Python Classes and Objects
No comments:
Post a Comment