How to convert ArrayList to array in java

Java arraylist to array conversionis used frequently by developers during application development. We have toArray() Method in java for convert arrayList to array in java.  toArray() method is overloaded in java. We have below ways to convert arraylist to array.

  1. public Object[] toArray();
  2. public <T> T[] toArray(T[] a);
  3. Public get() method;

ArrayList toArray() – convert to object array

This method does not take an argument and return array of object. We need to type cast array element to desired class .

package com.demo.arraylist;

import java.util.ArrayList;

public class ArrayListToArray {
	
   public static void main(String[] args) {
		/*
		 * Constructs an empty list with an initial capacity of ten.
		 */
		ArrayList<String> list = new ArrayList<String>();
		// add element in arraylist
		list.add("spring");
		list.add("php");
		list.add("html");
		list.add("java");
		// display arraylist
		System.out.println("arrayList is = " + list);
		
		/*
		 * Returns an array containing all of the elements in this list 
		 * in proper sequence (from first to last element). 
		 * The returned array will be "safe" in that no references to it
		 *  are maintained by this list. 
           (In other words, this method must allocate a new array). 
            The caller is thus free to modify the returned array. 
          This method acts as bridge between array-based and collection-based APIs.
		 */
		
		Object[] array = list.toArray();
         
		//print array element
        for (Object object : array) {
		      System.out.println(object);	
		}
		
		
   }

}

Output :

arrayList is = [spring, php, html, java]
spring
php
html
java

ArrayList toArray(T[] a) – convert to string array

In this method we pass one array. if

  • Array have enough space then list element fill in that
  • If Array have not enough size then it will create new array with list size
  • if Array have more space then it fill by element of list first then fill by null values.
import java.util.ArrayList;

public class ArrayListToArray2 {

	public static void main(String[] args) {
		
		ArrayList<String> list = new ArrayList<String>();
		// add element in arraylist
		list.add("spring");
		list.add("php");
		list.add("html");
		list.add("java");
		// display arraylist
		System.out.println("arrayList is = " + list);
		
        String[] arr = new String[list.size()]; 

        /*
         * Returns an array containing all of the elements in 
         * this list in proper sequence (from first to 
         * last element); the runtime type of the returned 
         * array is that of the specified array. If the list
         *  fits in the specified array, it is returned therein
         *  . Otherwise, a new array is allocated with the runtime
         *   type of the specified array and the size of this list. 
           If the list fits in the specified array with room to spare 
           (i.e., the array has more elements than the list), the
            element in the array immediately following the end of 
            the collection is set to null. (This is useful in determining
             the length of the list only if the caller knows that the list
              does not contain any null elements.)

         */
        
        arr = list.toArray(arr);
        //print array
		for (String str : arr) {
			System.out.println(str);
		}
		
	}
	
}

Output :

arrayList is = [spring, php, html, java]
spring
php
html
java

ArrayList get() – arraylist to string array java

Now we will iterate arraylist element one by one and set to array.

package com.demo.arraylist;

import java.util.ArrayList;

public class ArrayListToArray3 {

	public static void main(String[] args) {
		
		ArrayList<String> list = new ArrayList<String>();
		// add element in arraylist
		list.add("spring");
		list.add("php");
		list.add("html");
		list.add("java");
		
		//create array with list size
		String [] arr = new String[list.size()];
		
		//iterate list and set element in array
		for (int i = 0; i < list.size(); i++) {
			arr[i] = list.get(i);
		}
		
		System.out.println("Element in Array");
		
		//print array
		for (String str : arr) {
			System.out.println(str);
		}
		
		
	}
}

Output :

Element in Array
spring
php
html
java

 

Leave a Reply

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

87 − 84 =