Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-14-2008, 12:38 PM
Member
 
Join Date: May 2008
Posts: 8
John_28 is on a distinguished road
Casting problem
Hi,
can anyone tell me how can i cast Object[] to String[]
am trying this,

Object[] Value={abc,xyz}

String[] Value1=(String[]) Value1;

this not working if anyone knw how to do this plz help me.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-14-2008, 12:58 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
I don't know if that is allowed now in the latest jdk release...
My friend sent a request about that feature a few months ago.

As far as i know, you cannot just do that. You have to loop over their elements and do the casting.

here is an example,

Code:
public class test{ public static void main(String args[]){ Object[] t = {"I","am","who","am"}; String[] s = new String[t.length]; int x=0; for(Object temp:t){ s[x] = (String)temp;x++; } for(String temp:s){ System.out.println(temp); } } }
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-14-2008, 01:09 PM
Member
 
Join Date: May 2008
Posts: 8
John_28 is on a distinguished road
HI sukatoa,
I tried in ur way but following error occured

The java class could not be loaded. java.lang.UnsupportedClassVersionError: (Unsupported major.minor version 49.0)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-14-2008, 01:16 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Here is another example, using List

Code:
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; public class test{ static ArrayList<Object> a = new ArrayList<Object>(); static ArrayList<String> al; public static void main(String args[]){ Object[] t = {"I","am","a","Java","Programmer"}; a.addAll((Collection)Arrays.asList(t)); al = new ArrayList<String>((Collection)a); for(String castedInternally:al) System.out.println(castedInternally); } }
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-14-2008, 01:17 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
What jdk version did you use?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-14-2008, 01:24 PM
Member
 
Join Date: May 2008
Posts: 8
John_28 is on a distinguished road
Am using jdk1.6.0_04.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-14-2008, 01:29 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Im using update 3....

There should be a problem at your side....
those are compilable and runnable code....

But, my example has a different approach,
all objects inside the Array of object is a String representation.

How about that {abc,xyz}?

Are abc and xyz are Strings too? or has a String representation?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by sukatoa : 05-14-2008 at 03:56 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting an int value into a char kurtulas New To Java 2 02-16-2008 10:03 PM
Type Casting Help rhm54 New To Java 2 02-07-2008 02:06 PM
'Casting' couch !!!! ajaygargnsit Advanced Java 4 01-04-2008 06:54 PM
'Casting' couch !! ajaygargnsit New To Java 1 12-22-2007 03:05 PM
Casting leebee New To Java 5 08-10-2007 02:24 PM


All times are GMT +3. The time now is 11:48 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org