Java convert string to arraylist java example
package com.w3spoint;
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();
}
}
} |
package com.w3spoint; 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 |
Jai
Mahesh
Vivek
Naren
Vishal
Hemant
Download this example.