Saturday 9 May 2015

How To Copy ArrayList To Array

 How To Copy ArrayList To Array

 In this example we learn how to make a array from arraylist. we will use toArray() method for this purpose. This method return a array from arraylist 

import java.util.ArrayList;

public class ArrayListToArray {

    public static void main(String[] args) {
       
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
   
        String[] brr = new  String[arr.size()];
        //Returns an array containing all of the elements in this
         //list in proper sequence (from first to last element).
        arr.toArray(brr);
        for (int i = 0; i < brr.length; i++) {
              System.out.println(brr[i]);       
        }
    }
}

Output:

c
php
html
java

Related Posts: 

 


Storing Java Object In ArrayList




No comments:

Post a Comment