6

I have a comma separated String which i need to convert to ArrayList . I tried this way

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

    public class Test {

        public static void main(String[] args) {

            String CommaSeparated = "item1 , item2 , item3";

            ArrayList<String> items = (ArrayList)Arrays.asList(CommaSeparated.split("\\s*,\\s*"));

            for(String str : items)
            {
                System.out.println(str);
            }

        }

    }

Its giving me the Runtime Error as shown

Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
    at com.tradeking.at.process.streamer.Test.main(Test.java:14)

as i was trying to convert an List to arrayList by force .

2
  • 2
    try ArrayList<String> items = new ArrayList(Arrays.asList(CommaSeparated.split("\\s*,\\s*"))) Commented Aug 7, 2013 at 13:12
  • This question may be of use: stackoverflow.com/questions/4658867/… Commented Aug 7, 2013 at 13:16

5 Answers 5

19

The ArrayList returned by Arrays.asList is not java.util.ArrayList. It's java.util.Arrays.ArrayList. So you can't cast it to java.util.ArrayList.

You need to pass the list to the constructor of java.util.ArrayList class:

List<String> items = new ArrayList<String>(Arrays.asList(CommaSeparated.split("\\s*,\\s*")));

or, you can simply assign the result:

List<String> items = Arrays.asList(CommaSeparated.split("\\s*,\\s*"));

but mind you, Arrays.asList returns a fixed size list. You cannot add or remove anything into it. If you want to add or remove something, you should use the 1st version.

P.S: You should use List as reference type instead of ArrayList.

Sign up to request clarification or add additional context in comments.

2 Comments

It's super annoying that they used the same name ArrayList twice :)
varargs do not help to split a string - above claim is wrong.
2

You can't just cast objects around like they're candy. Arrays.asList() doesn't return an ArrayList, so you can't cast it (it returns an unmodifiable List).

However you can do new ArrayList(Arrays.asList(...));

2 Comments

It doesn't return an unmodifiable list, you can still call set(...) without any problems.
@arshajii Fair enough, let's call it "constant sized list".
1

Does it absolutely need to be an ArrayList? Typically you want to use the most generic form. If you're ok using a List just try:

List<String> items = Arrays.asList(...);

You can still iterate over it the same way you currently are.

Comments

1

Please try to use bellow code.

String[] temp;

    /* delimiter */
    String delimiter = ",";
    /*
     * given string will be split by the argument delimiter provided.
     */
    temp = parameter.split(delimiter);
    ArrayList<String> list = new ArrayList<String>();

    for (int l = 0; l < temp.length; l++)

    {

        list.add(temp[l]);

    }

Comments

1
String CommaSeparated = "item1 , item2 , item3";     
ArrayList<String> items = new ArrayList(Arrays.asList(CommaSeparated.split("\\s*,\\s*")));
for(String str : items)
{
    System.out.println(str);
}

Comments