Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Quicker way to cast array items?

Thread view: 
Paul Chapman - 20 Feb 2005 11:16 GMT
I have

   Object[] array;

I happen to know that each of the items of the array has class MyClass.  I
realize that I can't cast the whole array thus:

   (MyClass[]) array

But is there a quicker way than copying the whole array into a new
MyClass[], casting the items one at a time?

TIA.

Cheers, Paul
Anthony Borla - 20 Feb 2005 12:00 GMT
> I have
>
[quoted text clipped - 7 lines]
> But is there a quicker way than copying the whole array
> into a new MyClass[], casting the items one at a time?

Use:

   Object[] srcArray =
      new MyClass[]{null, null, null, null};

   MyClass[] targetArray =
      new MyClass[srcArray.length];

   System.arraycopy(srcArray, 0, targetArray, 0,
      srcArray.length);

I hope this helps.

Anthony Borla
Paul Chapman - 20 Feb 2005 13:17 GMT
Anthony,

>     Object[] srcArray =
>        new MyClass[]{null, null, null, null};
[quoted text clipped - 4 lines]
>     System.arraycopy(srcArray, 0, targetArray, 0,
>        srcArray.length);

Not quite copy-free, but better than a for-loop.  And it worked.  Thanks.

Cheers, Paul
Aleksander =?iso-8859-2?Q?Str=B1czek?= - 20 Feb 2005 13:32 GMT
> Anthony,
>>     Object[] srcArray =
[quoted text clipped - 7 lines]
>
> Not quite copy-free, but better than a for-loop.  And it worked.  Thanks.

If you have srcArray
    Object[] srcArray =
        new MyClass[]{null, null, null, null};
you can just cast it:
    MyClass[] targetArray = (MyClass[]) srcArray;

Copying is required if srcArray is created like tihs:
    Object[] srcArray = new Object[]{null};

Signature

Regards, Olek

Anthony Borla - 21 Feb 2005 02:19 GMT
> Anthony,
>
[quoted text clipped - 8 lines]
>
> Not quite copy-free, but better than a for-loop.

Indeed, I should think *very significantly better* than a 'for' loop ;) !

> And it worked.  Thanks.

More than glad to help get the job done :) !

Cheers,

Anthony Borla


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.