Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Doubt in toArray() method  RSS feed

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The SUN specification says

toArray(Object[] a) :
Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.


so i tried writing the below code.

public static void main(String[] args) {
ArrayList a = new ArrayList();
a.add("1");
a.add("2");
String[] proxy = {};
String[] stringArray = (String[])a.toArray( proxy );
}

But my doubt is,why do we need to typecast it to String array?The specification says that "the runtime type of the returned array is that of the specified array".So why do we need to typecast it again?

Kindly help me out.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the return type of List.toArray method is Object[]

At runtime, the toArray return method returns the same type of array
as is passed as parameter.

But this information is not known to the compiler.
The compiler only knows that the return type is Object []
and so it forces you to type-cast.

Does this make sense ?
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in Java 5, this method is declared as

<T> T[] toArray(T[] a)

If you know about generics in Java 5, you'll understand that this means the cast isn't necessary in this newer JDK version.
 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sat Nar

You can avoid the type cast if you dont catch the return value because the array passed to that method will be filled with appropriate values....

The code will look like this

ArrayList a = new ArrayList();
a.add("1");
a.add("2");
String[] proxy = new String[a.size()];
a.toArray( proxy ); // proxy will be filled appropriately

But if there is data type mismatch with any of the arraylist elements (If 4 of them is String and another being an Integer , ArrayStoreException will be thrown from the .toArray() method

Let me know if this is clear
 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
my sincere thanks to all for your replies.Its clear now.Verified the latest version and its been modified.And Sreenath,i got your point.Once again,my sincere thanks to all of you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!