0

Converting Strings in java into array lists

How do you convert strings from an input into an array list?

8th Nov 2019, 2:51 PM
HullaBrian
HullaBrian - avatar
8 ответов
+ 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); } }
8th Nov 2019, 3:09 PM
Avinesh
Avinesh - avatar
+ 8
Here is simple way String a ="hello"; List <String> list = new ArrayList<>(); Collections.addAll(list,a.split("")); System.out.println(list);
8th Nov 2019, 6:57 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 1
HullaBrian Do you want to store the characters of a string in an array list?
8th Nov 2019, 3:04 PM
Avinesh
Avinesh - avatar
+ 1
Awsome! thanks
8th Nov 2019, 3:11 PM
HullaBrian
HullaBrian - avatar
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);
8th Nov 2019, 2:58 PM
Odyel
Odyel - avatar
0
Thank you!
8th Nov 2019, 2:59 PM
HullaBrian
HullaBrian - avatar
0
Yes. How would i do that?
8th Nov 2019, 3:05 PM
HullaBrian
HullaBrian - avatar
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); }
8th Nov 2019, 3:20 PM
o.gak
o.gak - avatar