...
> I would like to
> know whether there is any other better/shorter way of doing such a
> conversion.
...
> Iterator Object1CollIter = Object1Coll.iterator();
> while ( Object1CollIter.hasNext() )
[quoted text clipped - 5 lines]
>
> Any suggestion for improvements in the above code?
If you really do want to convert between 2 polymorphically distinct types,
then no.
Other than using the Java1.5 syntactic short cut for iteration:
> Iterator Object1CollIter = Object1Coll.iterator();
> while ( Object1CollIter.hasNext() )
> {
> class1Obj = Object1CollIter.next() ;
is equivalent to:
for (Class1 obj : object1Coll.iterator()) {}
neat huh?!
> Class1 class1Obj = new Class1() ;
Oh, and that line is pointless. What are you doing with that object you just
instantiated? nothing!
a/ set it to null, or
b/ better reduce the scope by keeping the reference inside the loop
--
Mike W
qazmlp1209@rediffmail.com - 05 Mar 2006 01:21 GMT
Once the Collection -> Array conversion is completed, I have a case
later to do the reverse conversion i.e. converting from an array of a
object to Collection of an another object.
During this conversion, I just noticed that it is not possible to
initialize the Collection to a certain size(equivalent to
array.length). Is that really the case?
Roedy Green - 05 Mar 2006 03:18 GMT
>During this conversion, I just noticed that it is not possible to
>initialize the Collection to a certain size(equivalent to
>array.length). Is that really the case
does this answer your concern?
public ArrayList(Collection<? extends E> c) {
size = c.size();
// Allow 10% room for growth
int capacity = (int) Math.min((size*110L)/100,
Integer.MAX_VALUE);
elementData = (E[]) c.toArray(new Object[capacity]);
The constructor picks the right size automatically.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
IchBin - 05 Mar 2006 03:20 GMT
> Once the Collection -> Array conversion is completed, I have a case
> later to do the reverse conversion i.e. converting from an array of a
> object to Collection of an another object.
> During this conversion, I just noticed that it is not possible to
> initialize the Collection to a certain size(equivalent to
> array.length). Is that really the case?
I had to take 1.6 off my system because of space. There is an article
"Using the Desktop API in Java SE 6 (Mustang)" that touches on that API
and the author supplies a source link at the end of the article. Maybe
this could help?
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/mustang/desktop_api/
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
qazmlp1209@rediffmail.com - 13 Mar 2006 15:19 GMT
"object1Coll.iterator()" does not seem to work.
I have a method like this:
private otherClass[] convertFromMineToOthers( Collection<myClass>
myObjColl )
{
for ( myClass myObj : myObjColl.iterator() )
{
// processing
}
}
"Can only iterate over an array or an instance of java.lang.Iterable"
error is reported for "myObjColl.iterator()". Why does it occur? As I
checked Collection<> is an iterable type. So, I expected it to work.
But, unfortunately the above-mentioned error is reported.
Jacques-Olivier Haenni - 13 Mar 2006 15:27 GMT
Hi,
The correct syntax is:
for (MyClass myObj : myObjColl) {
...
}
Note the '.iterator()' was wrong.
Cheers,
Jacques-Olivier
>"object1Coll.iterator()" does not seem to work.
>I have a method like this:
[quoted text clipped - 13 lines]
>
>
qazmlp1209@rediffmail.com wrote in news:1141503457.827727.98670
@e56g2000cwe.googlegroups.com:
> I have a case where it is required to convert the Collection of objects
> of a class into an array of objects of an another class.
[quoted text clipped - 25 lines]
>
> Any suggestion for improvements in the above code?
Implement a method on Class1, that returns an appropriately-constructed
reference to an object of class2. This is more object-oriented, and allows
Class1 to hide more of its implementation-specific details.
Use Collection.toArray(new Class1[collectionSize]), then iterate this array
using a for loop. This should be faster than using an Iterator.
If Class2 is a superclass of Class1, you should be able to directly create
the array using Collection.toArray(new Class2[collectionSize]);
Cheers
GRB

Signature
---------------------------------------------------------------------
Greg R. Broderick [rot13] terto@oynpxubyvb.qlaqaf.bet
A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------