How to Convert String Array To List in java - java programming - learn java - java basics - java for beginners



 strarray to list

Learn Java - Java tutorial - strarray to list - Java examples - Java programs

String Array To List

  • The String Array To List shows how to convert Java String array to java.util.List object using Arrays.asList method.
  • To covert String array to java.util.List object, use List asList(String[] strArray) method of Arrays class.
  • Please note that list object created using this method cannot be modified. Any attempt to call add or delete method would throw UnsupportedOperationException exception.

Syntax:

String array[] to ArrayList<String>
 java string array list

Learn Java - Java tutorial - java string array list - Java examples - Java programs

sample code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
public class Strarray_to_List {
    public static void main(String args[]){
        String[] str = new String[]{"venkat", "prabhu", "arun", "wiki", "riya", "sam"};
        List li = (List) Arrays.asList(str);
        System.out.println("converted String array to List");
        for(int i=0; i < li.size(); i++){
            System.out.println(li.get(i));
    }
        List li1 = new ArrayList();
        Collections.addAll(li1, str);
    }
}

Output:

converted String array to List                           
                         venkat
                         prabhu
                         arun
                         wiki
                         riya
                         sam

Related Searches to how to String Array To List in java