I'm working in the android environment and have tried the following code, but it doesn't seem to be working.

String [] stockArr = (String[]) stock_list.toArray();

If I define as follows:

String [] stockArr = {"hello", "world"};

it works. Is there something that I'm missing?

share|improve this question
B.T.W., This is not related to Android - it is a pure Java matter – RonK Mar 21 '11 at 6:06
5  
use String [] stockArr = (String[]) stock_list.toArray(new String[0]); refer java doc here – Nishant Mar 21 '11 at 6:11

3 Answers

up vote 80 down vote accepted

Use like this.

    ArrayList<String> stock_list = new ArrayList<String>();
    stock_list.add("stock1");
    stock_list.add("stock2");
    String[] stockArr = new String[stock_list.size()];
    stockArr = stock_list.toArray(stockArr);
    for(String s : stockArr)
        System.out.println(s);
share|improve this answer
1  
Thanks your anser help me. – iDroid Explorer Feb 2 '12 at 5:10
7  
To provide an explanation as to what is going on here, the JVM doesn't know how to blindly downcast Object[] (the result of toArray()) to String[]. To let it know what your desired object type is, you can pass a typed array into toArray(). The typed array can be of any size (new String[1] is valid), but if it is too small then the JVM will resize it on it's own. – dhackner Apr 6 '12 at 7:41
2  
@dhackner - "... the JVM doesn't know how to blindly downcast Object[] to String[]". Or more precisely, it is not allowed to do that. If it could do that, it would violate Java type safety. – Stephen C Nov 8 '12 at 4:14
Thanks for the answer. – Code Enthusiastic Jan 26 at 12:53
Thanks for sharing, really works great. Simple yet very useful knowledge – Diljeet Feb 23 at 22:45
show 1 more comment

Try this

String[] arr = list.toArray(new String[list.size()]);
share|improve this answer

What is happening is that stock_list.toArray() is creating an Object[] rather than a String[] and hence the typecast is failing.

The correct code would be:

  String [] stockArr = stockList.toArray(new String[stockList.size()]);

or even

  String [] stockArr = stockList.toArray(new String[0]);

For more details, refer to the javadocs for the two overloads of List.toArray.

(From a technical perspective, the reason for this API behaviour / design is that an implementation of the List<T>.toArray() method has no information of what the <T> is at runtime. All it knows is that the raw element type is Object. By contrast, in the other case, the array parameter gives the base type of the array. (If the supplies array is big enough, it is used. Otherwise a new array of the same type and a larger size is allocated and returned as the result.)

share|improve this answer
2  
Why doesn't this work? String[] gs=(String[]) stockList.toArray(); – Ashwin Jun 29 '12 at 10:02
@Ashwin - that is explained in the first line of my Answer. And if you want the legalistic "why", it is because this is how the toArray() is specified by the javadoc. – Stephen C Aug 22 '12 at 6:54

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.