This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ArrayList Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Conversion" Watch "ArrayList Conversion" New topic

ArrayList Conversion

Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Everyone,

I know that this question was posted several times but I can't find the answer to my query. I need to convert an arraylist to String array.

But I keep on having error java.lang.ClassCastException

I thought calling myArrayList.toArray() would return an Object[] array so I did cast it to (String[]) but I still get the error.

I did try to experiment and adjusted my code below and it works fine but with Object as my data type.

Also, I found a resource on the Search on javaranch and found a good example on arraylist to array conversion and it works well.

But I'm just wondering why my first code does not work whereas I declare my arrayList as String. I know I'm missing something here. Please help.

Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
John William

Joined: Sep 20, 2007
Posts: 5
I thought Array don't convert with the method below:

Sting[] anArray = (String[])myArrayList.toArray();

In my Opinion,once you declared an Array,it will not convert data type.

In Addition,your code may write in this way:

Good Luck
Rob Spoor

Joined: Oct 27, 2005
Posts: 20077

Originally posted by mark reyes:

But I'm just wondering why my first code does not work whereas I declare my arrayList as String. I know I'm missing something here. Please help.

Look at the API.

toArray() returns an Object[]. This is created as an Object[], not a String[]. Therefore, you cannot cast it to String[].

toArray(T[]) returns a T[] (for whatever T you want). So if you want toArray to return a String[], you need this version and pass it any String[] array.
Performance dictates that you should use either an array that will be large enough for all elements (so using the collection size is good) or a cached version of an empty array. This is because if the passed array is not large enough a new array will be created. Imagine creating an array for the sake of getting the right type of array, then having another array be created within toArray. Either skip the creation of the passed array (cached empty array) or the creation within toArray (by passing an array that is large enough).

How To Ask Questions How To Answer Questions
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: ArrayList Conversion
It's not a secret anymore!