1

Possible Duplicate:
Assigning an array to an ArrayList in Java

How can I convert a array of strings like :

String logsSplitFromUrdu[] = fromTheRecentLogFile.split("غزل");

into a ArrayList<String> ?

Edit:

I was doing like when I get an error :

ArrayList<String> time = (ArrayList<String>)Arrays.asList(logsSplitFromUrdu);

EDIT :

Exception that I get when do the above :

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
    at Tester.main(Tester.java:26)
Community
  • 1
  • 1
Y.E.P
  • 1,157
  • 6
  • 18
  • 39

6 Answers6

2

Use the method Arrays.asList() from the Arrays class. It will return a List of the correct generic type (String in this case):

List<String> list = Arrays.asList(logsSplitFromUrdu);

If you absolutely need an ArrayList<String> and a List<String> won't do, then write one additional line after the previous one:

ArrayList<String> arraylist = new ArrayList<String>(list);
Óscar López
  • 225,348
  • 35
  • 301
  • 374
2

Take a look at Arrays#asList. This method returns a fixed size list.

So, if you are ok with fixed size list, then you can use this: -

List<String> fixedList = Arrays.asList(logsSplitFromUrdu);

But, if you want to modify your newly created List, you need to create it like this: -

ArrayList<String> modifiableList = 
             new ArrayList<String>(Arrays.asList(logsSplitFromUrdu))

BUT you should not do it like this: -

ArrayList<String> modifiableList = 
                  (ArrayList<String>)Arrays.asList(logsSplitFromUrdu))

It will give you ClassCastException, because, the list returned from Arrays.asList is of type: - java.util.Arrays.ArrayList and it cannot be cast to : - java.util.ArrayList

UPDATE: - java.util.Arrays#ArrayList is a static class defined in Arrays class.

Rohit Jain
  • 203,151
  • 43
  • 392
  • 509
0

The return type of

Arrays.asList(logsSplitFromUrdu);

is List<String> and cannot be cast to ArrayList<String>. Instead you can use:

List<String> myList = Arrays.asList(logsSplitFromUrdu);

which returns an unmodifiable list. To get a modifiable list you could use:

ArrayList<String> modifyiableList = new ArrayList<String>(myList); 
modifyiableList.add("...");
Reimeus
  • 155,977
  • 14
  • 207
  • 269
0

U have to use asList method of java.util.Arrays, refer the docs here

Akhilesh
  • 1,300
  • 3
  • 15
  • 19
0

Use the Arrays.asList() method to convert the array into ArrayList

String logsSplitFromUrdu[] = fromTheRecentLogFile.split("غزل");

ArrayList<String> arList = new ArrayList<String>(Array.asList(logsSplitFromUrdu));
Kumar Vivek Mitra
  • 32,904
  • 6
  • 47
  • 76
0

You can simply use a for loop to copy all of the elements from the array into a new ArrayList like so:

ArrayList<String> logsList = new ArrayList<String>();
for(int i = 0; i < logsSplitFromUrdu.length; i++)
{
    logsList.add(logsSplitFromUrdu[i]);
}

And after this logsList will contain all of the elements in your original array.

Tristan Hull
  • 350
  • 1
  • 3
  • 12