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 .

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    try `ArrayList items = new ArrayList(Arrays.asList(CommaSeparated.split("\\s*,\\s*")))` – user902383 Aug 07 '13 at 13:12
  • This question may be of use: http://stackoverflow.com/questions/4658867/why-does-arrays-aslist-return-its-own-arraylist-implementation – Adil Aug 07 '13 at 13:16

5 Answers5

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
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(...));

Kayaman
  • 72,141
  • 5
  • 83
  • 121
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.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
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);
}
Will Eddins
  • 13,628
  • 5
  • 51
  • 85
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]);

    }
nilesh patel
  • 834
  • 1
  • 5
  • 10