Languages
[Edit]
EN

Java - convert String to ArrayList

6 points
Created by:
Root-ssh
175340

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]

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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