• 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

collection-toArray

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends
a collection can store all type of objects;
vector v=new vector();
v.add(new integer(1));
v.add(new string("one");
v.add(new integer(10));
now is it possible to convert v into an array;if so means wat data type-array elements will represent , as collection has objects;

thanks
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sri jaisi:
hi friends
a collection can store all type of objects;
now is it possible to convert v into an array;if so means wat data type-array elements will represent , as collection has objects;

thanks




Yes Dear,
It is possible to convert any collection into an array , but the array will be of Object Type. If you want to use it as an array elements then you need to typecast those objects.

I must show you by an Example:



I think now you get the solution.

If still any concern then let us know.
[ September 29, 2006: Message edited by: Ankur Sharma ]
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ September 29, 2006: Message edited by: Chetan Parekh ]
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chetan Parekh:




Well I might not use anytime this one void copyInto(Object[] anArray) . Because in this method I need to take care the length of arrray If we use the below example then we can easily understand.



Well the output is ;

Value0:String1
Value1:String2
Value2:String3
Value3:String4
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.util.Vector.copyInto(Vector.java:166)
at Sample.main(Sample.java:168)
Exception in thread "main"



That's why I prefer using toArray() method.
[ September 29, 2006: Message edited by: Ankur Sharma ]
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ankur, you are right.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Advanced?

Moving to Java in General (Beginner).
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is is possible to get the toArray method to return an Array of the appropriate types -- if you pass it in first.



Granted, you have to know how large to make the array, but that's what the .size() method does.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel McNary:
Is is possible to get the toArray method to return an Array of the appropriate types -- if you pass it in first.



Granted, you have to know how large to make the array, but that's what the .size() method does.




Joel I appreciate your effort,
But here you are using different method from what I have used.

There are two version of this method.
1. Object[] toArray()--That what I was using
2. Object[] toArray(Object a[])--This is what you are using

And offcourse when you are passing an array then array must have size and if we need to give size while initiliazing any array.
[ September 29, 2006: Message edited by: Ankur Sharma ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankur Sharma:



Joel I appreciate your effort,
But here you are using different method from what I have used.

There are two version of this method.
1. Object[] toArray()--That what I was using
2. Object[] toArray(Object a[])--This is what you are using

And offcourse when you are passing an array then array must have size and if we need to give size while initiliazing any array.

[ September 29, 2006: Message edited by: Ankur Sharma ]



Yes, he is using a different method, one that allows him to specify the type of the array. Why do you seem to have some sort of objection to specifying the size of the array? As Joel has demonstrated it's a trivial use of size() to create an array of the appropriate length. Personally I would take his example and break it into two lines just to avoid the cast and make it easier to read, but it's a better solution if you want a more specific type of array than Object. Creating the array yourself is in my never to be humble opinion a superior solution to being forced to cast every time you read from it.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. Most IDEs have a single-keystroke command for generating Ken's line of code. I use it all the time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic