Languages
[Edit]
EN

Java - convert String to ArrayList

6 points
Created by:
Root-ssh
115400

In Java, we can convert String to ArrayList in following ways.

1. Split String by comma

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example1 {

    public static void main(String[] args) {

        String text = "ab,cd,ef";
        List<String> list = new ArrayList<>(Arrays.asList(text.split(",")));
        System.out.println(list); // [ab, cd, ef]
    }
}

Output:

[ab, cd, ef]

2. Split String by every letter

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example2 {

    public static void main(String[] args) {

        String text = "abc";
        List<String> list = new ArrayList<>(Arrays.asList(text.split("")));
        System.out.println(list); // [a, b, c]
    }
}

Output:

[a, b, c]

 

Java - String conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join