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 08-27-2008, 10:03 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
willemjav is on a distinguished road
type casting array?
Is there a way to typecast an array? I have got an array of MidiDevice.Info objects which I would like to cast into an array of strings (I have a getter which return type is an array of string)

Code:
public String[] getIns() { if (mmj) return de.humatic.mmj.MidiSystem.getInputs(); else return ins; // javax.sound.midi.MidiDevice.Info[] ins; } // which gives, of course an error
getMidiDeviceInfo
public static MidiDevice.Info[] getMidiDeviceInfo()
Obtains an array of information objects representing the set of all MIDI devices available on the system. A returned information object can then be used to obtain the corresponding device object, by invoking getMidiDevice.

Returns:
an array of MidiDevice.Info objects, one for each installed MIDI device. If no such devices are installed, an array of length 0 is returned.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-27-2008, 10:14 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
This sounds like a very bad idea.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-27-2008, 10:22 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
willemjav is on a distinguished road
So I did this and it works?

public Object[] getIns() {
if (mmj) return de.humatic.mmj.MidiSystem.getInputs();
else return ins;
}
or are there some bad consequences too?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-27-2008, 10:22 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Quote:
an array of MidiDevice.Info objects which I would like to cast into an array of strings
The objects in the array are either one type or another. You can NOT use casting to convert them.
If there is a way to get a String from an Info object, you could use a loop to extract those strings into a String[] array.

How do you use the array returned by getIns()? Does it have to be String[]? Change the references to use the toString() method of its contends and add a toString() method to the Info object to return what you want.
This might be a place to use an interface to keep control of the usage of the type returned.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-28-2008, 12:20 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
willemjav is on a distinguished road
Norm, I use the return value to fill a combo:

ib = new JComboBox(setm.getIns());

It works when using object, because string casts up (or down)?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-28-2008, 12:25 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
I guess it works because the toString() method of the String class overrides the one in Object to return the string allowing the JComboBox class to get at the strings.
JComboBox has a constructor that takes Object[]
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
[SOLVED] Cast string type to int type GilaMonster New To Java 9 09-17-2008 12:43 PM
Using reflection to check array type and length Java Tip java.lang 0 04-23-2008 10:15 PM
Using reflection to check array type and length Java Tip java.lang 0 04-14-2008 10:42 PM
Type Casting Help rhm54 New To Java 2 02-07-2008 02:06 PM
Need help with creating array of type object riz618 New To Java 3 01-29-2008 08:14 AM


All times are GMT +3. The time now is 09:41 AM.


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