Convert ArrayList to Arrays
Answer: Many time in programming situation arises to convert ArrayList to Arrays and java API provides many ways to convert from ArrayList to Arrays. Please have java example below where I will show three different ways to convert ArrayList to Arrays:
package com.javahonk.arraylistvector; import java.util.ArrayList; import java.util.List; public class ArrayListToArrays { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Java"); list.add("Honk"); list.add("Test"); list.add(null); list.add(null); list.add("Software"); list.add("Software"); list.add("IT"); System.out.println("Example 1:\n"); Object [] array = list.toArray(); for (int i = 0; i < array.length; i++) { Object object = array[i]; System.out.println(object); } System.out.println("\nExample 2:\n"); String arrays[] = list.toArray(new String[0]); for (int i = 0; i < arrays.length; i++) { String string = arrays[i]; System.out.println(string); } System.out.println("\nExample 3:\n"); String arrayString [] = new String[list.size()]; for (int i = 0; i < list.size(); i++) { String string = list.get(i); arrayString[i] = string; } for (int i = 0; i < arrayString.length; i++) { String string = arrayString[i]; System.out.println(string); } } }
Output:
That’s it Convert ArrayList to Arrays