0
Converting Strings in java into array lists
How do you convert strings from an input into an array list?
8 Antworten
+ 3
import java.util.*;
public class Program
{
public static void main(String[] args) {
String str = "sololearn";
List<Character> list = new ArrayList<>();
for(int i=0;i<str.length();i++){
list.add(str.charAt(i));
}
System.out.println(list);
}
}
+ 8
Here is simple way
String a ="hello";
List <String> list = new ArrayList<>();
Collections.addAll(list,a.split(""));
System.out.println(list);
+ 1
HullaBrian Do you want to store the characters of a string in an array list?
+ 1
Awsome! thanks
0
Don't think you can convert it but you can add it to an ArrayList
Use .add()
ArrayList<String> ary = new ArrayList<String>();
String word = input.next();
ary.add(word);
0
Thank you!
0
Yes. How would i do that?
0
If you want to put the input strings (separated by space for example) into an arraylist, you can do it like this.
ArrayList<String> strList = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String s = scan.next();
strList.add(s);
}