Q
qazmlp1209
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.
I have come up with the following sample code so far. I would like to
know whether there is any other better/shorter way of doing such a
conversion.
------------------------
// Object1Coll is of type Collection<Class1>
final int collectionSize = Object1Coll.size() ;
Class2[] class2Arr = new Class2[ collectionSize ] ;
int index = 0 ;
Class1 class1Obj = new Class1() ;
Iterator Object1CollIter = Object1Coll.iterator();
while ( Object1CollIter.hasNext() )
{
class1Obj = Object1CollIter.next() ;
class2Arr[ index ] = new Class2( class1Obj.getMemVal1()
, class1Obj.getMemVal2() ) ;
++index ;
}
of a class into an array of objects of an another class.
I have come up with the following sample code so far. I would like to
know whether there is any other better/shorter way of doing such a
conversion.
------------------------
// Object1Coll is of type Collection<Class1>
final int collectionSize = Object1Coll.size() ;
Class2[] class2Arr = new Class2[ collectionSize ] ;
int index = 0 ;
Class1 class1Obj = new Class1() ;
Iterator Object1CollIter = Object1Coll.iterator();
while ( Object1CollIter.hasNext() )
{
class1Obj = Object1CollIter.next() ;
class2Arr[ index ] = new Class2( class1Obj.getMemVal1()
, class1Obj.getMemVal2() ) ;
++index ;
}