| Author |
Need more info on toArray(Object[])
|
Krish Pinnamaneni
Greenhorn
Joined: Mar 16, 2005
Posts: 22
|
|
Hi, Can any body tell me with example regarding following method List.toArray(Object[]). I know that List.toArray() returns generalized Object[]. In Advance, Thanks, Krish
|
 |
Animesh Shrivastava
Ranch Hand
Joined: Jul 19, 2004
Posts: 298
|
|
Krish, Just try having this in ur main method: U will come to know the difference. In the first method "arList.toArray()", the toArray() method returns Object[]. U cannot convert Object[] to a String[], right? its the same as invoking this: Which has to throw a run time error. Now "arList.toArray(new String[3])". In this method u explicitly specify the run time type of the array so that it returns of the same kind. Its like invoking I hope thats clear now [ March 31, 2005: Message edited by: Animesh Shrivastava ]
|
 |
Krish Pinnamaneni
Greenhorn
Joined: Mar 16, 2005
Posts: 22
|
|
Hi Animesh Shrivastava , Thanks a lot for spending your most valuable time to give response. Thanks, Krish
|
 |
Krish Pinnamaneni
Greenhorn
Joined: Mar 16, 2005
Posts: 22
|
|
Hi Animesh, Please correct me if i am wrong. ArrayList arList = new ArrayList(); arList.add("1"); arList.add("2"); String[] str = (String[])arList.toArray(); I know that above code will not be compiled.I need more info. why the code is not being compiled?. My understandings: we are adding String's "1" "2" to List.since List.add(Object) those strings are upcasted to generalized Object(i.e. java.lang.Object).so List.toArray() returns Object[].But each element of Object[] can be down casted to String since originally we added String to List. I don't understand why it is giving compile time error?.Please validate my understandings Thanks, Krish.
|
 |
Animesh Shrivastava
Ranch Hand
Joined: Jul 19, 2004
Posts: 298
|
|
Krish,
String[] str = (String[])arList.toArray();
This shouldnt give compiler error. It shouldnt give any error, thats fine, But it will give run time (ClassCastException) error, for which i have already given the explanation in my previous post. But if ur code was
String[] str = arList.toArray();
U would have got a compilation error because its toArray() method returns Object[] so u need to cast it to make String[].
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
hey ranchers.. i m still not convienced with animesh... that why classcastException will be thrown at run time because till the point we add string to list as object and they are store as bject ... so why String[] str = (String[])arList.toArray(); in this we are also casting... if anybody pls explain me...it would be gr8 in depth it gives error and why not this String[] str1 = (String[])arList.toArray(new String[3]);
|
Thanks and Regards,<br />Amit Taneja
|
 |
Amit Das
Ranch Hand
Joined: Mar 05, 2005
Posts: 206
|
|
Suppose: Object[] objArr = { new String("a"), new String("b") }; String[] strArr = (String[])objArr; //ClassCastException reason is simple.....objArr is a ref. variable of type Object[] and the object ref. it caontains is also of type Object[], even at runtime it refers to an object which is of type Object[] only(but objArr instanceof String[] is false, so runtime exception.... if it was : Object obj = new String("Hello"); String str = (String)obj; //perfectly fine as : //obj instanceof String is true hope this helps.... thanx amit
|
 |
Soni Prasad
Ranch Hand
Joined: Mar 09, 2005
Posts: 97
|
|
Line 3 throws ClassCastException but line 4 does not. Object[] toArray() returns Object[] but Object[] toArray(Object a[]); returns String[] as Object[].
|
SCJP 1.4, SCBCD 1.3
|
 |
 |
|
|
subject: Need more info on toArray(Object[])
|
|
|