How to return 2 values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devinaren
    New Member
    • Nov 2007
    • 4

    How to return 2 values

    How can i return 2 values in below code:

    I want to return both x and y. How can I do that?

    public boolean populateTab()
    {
    int tabIndex = _tabPanel.getSe lectedIndex();
    switch (tabIndex)
    {
    case BulletinConstan ts.INPUT_TAB_IN DEX:
    _bulletinItemTa b.init(null);
    x = true;
    y = false;
    Log.enter(getCl ass(),"Tab moved to Input");
    break;
    case BulletinConstan ts.RELAY_TAB_IN DEX:
    _relayTabAdapte r.setLogger(get Logger());
    _bulletinRelayT ab.init();
    Log.enter(getCl ass(),"Tab moved to Relay");
    y = true;
    x = false;
    break;
    default:
    break;
    }
    return x;// I want to return both x and y;
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by devinaren
    How can i return 2 values in below code:

    I want to return both x and y. How can I do that?

    public boolean populateTab()
    {
    int tabIndex = _tabPanel.getSe lectedIndex();
    switch (tabIndex)
    {
    case BulletinConstan ts.INPUT_TAB_IN DEX:
    _bulletinItemTa b.init(null);
    x = true;
    y = false;
    Log.enter(getCl ass(),"Tab moved to Input");
    break;
    case BulletinConstan ts.RELAY_TAB_IN DEX:
    _relayTabAdapte r.setLogger(get Logger());
    _bulletinRelayT ab.init();
    Log.enter(getCl ass(),"Tab moved to Relay");
    y = true;
    x = false;
    break;
    default:
    break;
    }
    return x;// I want to return both x and y;
    }
    1.) Use code tags when posting code
    2.) Methods can only return one value in Java. You can however return an array or a composite object e.g you could define a class called TwoBooleans which stores two booleans and then return that instead.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Why not 'steal' from C++ what they've done correct? I sometimes use the following
      little helper class for the purpose of 'bundling' more than one value:

      [code=java]
      public class Pair<T, U> {

      public final T t;
      public final U u;

      public Pair(T t, U u) {

      this.t= t;
      this.u= u;
      }
      }
      [/code]

      We could discuss whether or not both elements should be final; I think they
      should be final.

      kind regards,

      Jos

      Comment

      • java2986
        New Member
        • Feb 2009
        • 2

        #4
        hi i'm new

        i use something like this:
        [code=java]
        public static Object[] wharever() {
        Object object[] = new Object[3];
        int x = 0;
        String y = "0";
        Boolean z=true;
        object[0] = x;
        object[1] = y;
        object[2]=z;
        return object;
        }

        public static void main(String... args) {
        Object object[] = wharever();
        int y = Integer.parseIn t( object[1].toString());
        int x = Integer.parseIn t( object[2].toString());
        boolean z=Boolean.parse Boolean(object[3].toString());
        }
        [/code]
        You can return an Object array and cast to any type you want.
        Last edited by Nepomuk; Feb 17 '09, 06:19 PM. Reason: Please use [CODE] tags

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by java2986
          i use something like this:
          Please compile and run/test your code before you post it; there's an ArrayIndexOutOf BoundsException in your code.

          kind regards,

          Jos

          Comment

          • java2986
            New Member
            • Feb 2009
            • 2

            #6
            ..this one is fix

            Sorry i modified the the method but din't do it to the main

            Code:
            public static Object[] wharever() {
            Object object[] = new Object[3];
            int x = 0;
            String y = "1";
            Boolean z=true;
            object[0] = x;
            object[1] = y;
            object[2]=z;
            return object;
            }
            
            public static void main(String... args) {
            Object object[] = wharever();
            int y = Integer.parseInt( object[0].toString());
            int x = Integer.parseInt( object[1].toString());
            boolean z=Boolean.parseBoolean(object[2].toString());
            System.out.println(y);
            System.out.println(x);
            System.out.println(z);
            }
            i wass accesing the posicion that don't exist (boolean z=Boolean.parse Boolean(object[3].toString());)

            Comment

            Working...