top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert ArrayList to Array and Array to ArrayList in java?

0 votes
539 views
How to convert ArrayList to Array and Array to ArrayList in java?
posted Feb 5, 2018 by Ammy Jack

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

convert ArrayList to Array and Array to ArrayList in Java:

public class LengthVsSizeArrayList {  
    public static void main(String[] args) {  
        //creating Arraylist  
        List<String> fruitList = new ArrayList<>();  
        //adding String Objects to fruitsList ArrayList  
        fruitList.add("Mango");  
        fruitList.add("Banana");  
        fruitList.add("Apple");  
        fruitList.add("Strawberry");  
        fruitList.add("Pineapple");  
        System.out.println("Converting ArrayList to Array" );  
        String[] item = fruitList.toArray(new String[fruitList.size()]);  
        for(String s : item){  
            System.out.println(s);  
        }  
        System.out.println("Converting Array to ArrayList" );  
        List<String>l2 = new ArrayList<>();  
        l2 =  Arrays.asList(item);  
        System.out.println(l2);  
    }  
}  

Output:

Converting ArrayList to Array
Mango
Banana
Apple
Strawberry
Pineapple
Converting Array to ArrayList
[Mango, Banana, Apple, Strawberry, Pineapple]

answer Feb 9, 2018 by Frank Lee
...