How to convert String[] to ArrayList<String>

 Posted On  | Yashwant Chavan 

In this tutorial, You will learn how to convert String[] to ArrayList.

String[] to ArrayList<String>

Refer below steps

  • Create String array with some elements.
  • Arrays.asList() method returns a fixed-size List backed by the specified array.
  • Print each element from ArrayList using for loop.
package com.technicalkeeda.app;

import java.util.Arrays;
import java.util.List;

class ConvertArraytoListExample {

    public static void main(String[] args) {

        String[] cities = new String[] {
            "Pune",
            "Mumbai",
            "Delhi"
        };
        List < String > list = Arrays.asList(cities);

        for (String city: list) {
            System.out.println("List Elements:- " + city);
        }
  
    }

}

Output

List Elements:- Pune
List Elements:- Mumbai
List Elements:- Delhi


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0314 seconds.