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)
8
  • 1
    What have you tried? Commented Oct 14, 2012 at 14:51
  • 2
    Did you go through the documentation of : - Array#asList Commented Oct 14, 2012 at 14:52
  • 1
    What error? Or is guessing part of the fun? Commented Oct 14, 2012 at 14:54
  • @AndrewThompson error that it cannot be casted Commented Oct 14, 2012 at 14:54
  • Please copy/paste the new information into the question as an edit, and use code formatting on it. Commented Oct 14, 2012 at 14:55

6 Answers 6

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);
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.

3
  • java.util.List.ArrayList I doubt if it even exists ! Commented Oct 14, 2012 at 15:09
  • @Y.E.P Sorry its. java.util.Arrays.ArrayList. Will modify it.. Commented Oct 14, 2012 at 15:11
  • @Y.E.P See this excellent Post: - stackoverflow.com/questions/4658867/… for more information about this Commented Oct 14, 2012 at 15:14
0

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

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));
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.

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("...");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.