Tutorialspoint examples

Learn java tutorial with examples for beginners online

Convert string to arraylist java


Java convert string to arraylist java example

package com.tutorialspointexamples;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class StringToArrayList {
  public static void main(String args[]){
	try {
		String namesString = "Jai,Mahesh,Vivek,Naren,Vishal,Hemant";
                String str[] = namesString.split(",");
 
                List<String> strList = new ArrayList<String>();
                strList = Arrays.asList(str);
                for(String string : strList){
                   System.out.println(string);
                }
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
}

Output:

Jai
Mahesh
Vivek
Naren
Vishal
Hemant

Download this example.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status