How to split String into ArrayList in Java

2 Answers

0 votes
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        String s = "java c++ php python";
        String[] arr = s.split(" ");
        
        List<String> lst = Arrays.asList(arr);
        System.out.println(lst);
    }
}
 
 
 
/*
run:
 
[java, c++, php, python]

*/

 



answered Aug 1, 2020 by avibootz
0 votes
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        String s = "java software programming";

        List<String> list = new ArrayList<String>(Arrays.asList(s.split(" ")));

        System.out.println(list);  
    }
}




/*
run:

[java, software, programming]

*/

 



answered Nov 12, 2021 by avibootz

Related questions

1 answer 239 views
1 answer 121 views
2 answers 172 views
1 answer 106 views
106 views asked Oct 14, 2023 by avibootz
1 answer 195 views
1 answer 156 views
...