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 215 views
1 answer 111 views
2 answers 154 views
1 answer 95 views
95 views asked Oct 14, 2023 by avibootz
1 answer 184 views
1 answer 144 views
...