Languages
[Edit]
EN

Java - convert String to ArrayList

6 points
Created by:
Root-ssh
6631

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

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.