Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,064
    Rep Power
    8

    Default 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)

    Java 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This sounds like a very bad idea.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,064
    Rep Power
    8

    Default

    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?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,064
    Rep Power
    8

    Default

    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)?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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[]

Similar Threads

  1. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  2. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:15 PM
  3. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 PM
  4. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM
  5. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •