• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I the below program i am getting class cast excetption at line1

if i remove the comments of that line i am getting ClassCastException
why is it wrong?

i am assigning the reference of the Object array to a String array .

What mistake i am doing here?


import java.util.*;

class toArray
{
public static void main(String... args)
{
Collection<String> c1=new ArrayList<String>();
c1.add("kumar");
c1.add("anil");
c1.add("dhora");
c1.add("lalam");

String s1[]=new String[20];
Object[] o=c1.toArray();

System.out.println("The value is :");
/*s1=(String[])o; //line 1
for(Object s )
System.out.println(s);*/

for(Object s )
System.out.println(s);

System.out.println("the elements in the array is : "+c1);
}
}


Please Explain this

Thanks
Anil kumar
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,


toArray() actually returns reference to the Object array so therefore casting Object to String will cause CCE. It wont give compiler error because String IS-A(n) Object.

You can try another version of the toArray(...) for your purpose:

 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Chandra
----------------------------------------------------------------------
String [] sarr = c1.toArray(new String[0]);
-----------------------------------------------------------------------
I have worked with this.It is working perfectly.

What is CCE?
is it CompilerCheckedException or not?

What is wrong with this statement ?
--------------------------------
/*s1=(String[])o; //line 1
---------------------------

I could not understnad
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Anil,

I meant CCE by ClassCastException (sorry for using sort form).


What is wrong with this statement ?
--------------------------------
/*s1=(String[])o; //line 1
---------------------------

I could not understnad



Object o1 = new Object();
String s1 = (String)o1; //ClassCastException is thrown, (got it?)


Do you get the following two lines:
Object[] o1 = new Object[2];
o1[0] = "hello";
o1[1] = "howdy";
String[] = s1 =(String[])o1;

Can you assign reference to the Object array to the reference to the String array? No!

Even if each elements of the o1 refers to the String object. This casting is not OK hence throws CCE.

Here is the definition of the both the methods:



 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

chandra

I got it

But what does this statement mean?
-----------------------------------------------------------------------
If the list fits in the specified array with room to spare (i.e., the * array has more elements than the list), the element in the array * immediately following the end of the collection is set to * <tt>null</tt>.
---------------------------------------------------------------------
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anil kumar:
Thanks

chandra

I got it

But what does this statement mean?
-----------------------------------------------------------------------
If the list fits in the specified array with room to spare (i.e., the * array has more elements than the list), the element in the array * immediately following the end of the collection is set to * <tt>null</tt>.
---------------------------------------------------------------------



You get even if you pass array of 0 length. A new array is created to fit
the size of the list.

If the size of the passed array is more than the number of elements in the list, a null is added after the last list element of the array to. This is only helpful in determining the length of the array (if the user knows
there are no null following the end of the list).

Suppose List has:
["kumar","sumit","pavan","sawan"]
and the array length is 10 (that you passed to the method)

s1[0] ="kumar"
s1[1] ="sumit"
s1[2] ="pavan"
s1[3] ="sawan"
s1[4] = null <==== added by the method toArray, to inform you list elements
complete just before this element.

In this case you know there are no null in the list, so easy to determine the size of the array (how many list elements are in it)

Otherwise if you have null in the list as:
["kumar","sumit","pavan","sawan",null,null,null]
The above mechanism is not for this case, because there are null already present in the list. it wont help, when toArray method adds null as I said before.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Chandra

I have tried the same thing but i didn't get yesterday.

Now i come to know what mistake i did.

I had declared the Object array like this
Object[] o=new Object[3];

if the list size is say 3
i didn't get the remainig 27 values as null.

But i have declate the array like this
Object[] o=list.toArray(new Object[30]);

Now i will get the remaining 27 nulls.

Thanks you


Anil kumar
[ April 29, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think directly casting object array to String array is not possible, we have to iterate over Object array and then cast one by one.

Thanks,

Abdul Mohsin
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A slightly modified version of the same code to verify my understanding of toArray().

* If the array supplied to toArray() is smaller than the # of list items, then a new array will be returned with all the contents copied. That's why even if you pass an array of 0 size new String[0], it returned the complete list.
In this case the address of the array that was passed in is different from the one returned by toArray().

* If the array supplied is larger then the all the items will be copied to the SAME array with the remaining array elements set to null. In this case the no new array will be created and the address of the array passed will be the same as the one that gets returned by the toArray().


[ April 30, 2007: Message edited by: M Krishnan ]
[ April 30, 2007: Message edited by: M Krishnan ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abdul Mohsin:
I think directly casting object array to String array is not possible, we have to iterate over Object array and then cast one by one.

Thanks,

Abdul Mohsin



Hi Abdul,

Didn't you look at the other overloaded version of the toArray(...) method.
You need not to make any cast, just pass the array to it, it will return the same type of the array.

Even if the size of the passed array is small, the toArray(...) method will
create a new array for you with the size required to contain all the list elements. If the size, of the array you passed to it is big that the number of elements of the list, the toArray(...) method will fill all the list elements to it and then insert a null into the array so that you could get where the list elements end in the array.

Remember: Array of references are initialized with nulls by default. So when you encounter the first null, you can think like the list elements ends just before this null element. (Exception to this assumption is when you have null in the list elements too as elements)
[ April 30, 2007: Message edited by: Chandra Bhatt ]
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya that version is there,
Thanks for pointing me.

Abdul Mohsin
 
reply
    Bookmark Topic Watch Topic
  • New Topic