return two variables from one method

前端 未结 5 1765
小蘑菇
小蘑菇 2021-02-08 04:30

How to write the following code correctly?

public String toString(int[] position, int xOffset, int yOffset) {
    String postn = String.format(\"[%d,%d]\", posit         


        
相关标签:
5条回答
  • 2021-02-08 05:01

    When using Java 8 you could make use of the Pair class.

    private static Pair<String, String> foo (/* some params */) {
        final String val1 = "";  // some calculation
        final String val2 = "";  // some other calculation
    
        return new Pair<>(val1, val2);
    }
    

    Otherwise simply return an array.

    0 讨论(0)
  • 2021-02-08 05:04

    There are four method which I can think of to implement this scenario:

    1. Using a class

    Create a class with having all the attributes which you want to return, in your case it can be

    public class Sample {
       private String postn;
       private String movm;
       public String getPostn() {
            return postn;
       }
       public void setPostn(String postn) {
        this.postn = postn;
       }
       public String getMovm() {
           return movm;
       }
       public void setMovm(String movm) {
           this.movm = movm;
       }    
    }
    

    and change your method to include this class's object and set the values in these attributes. Change the return type of the method to Class name e.g. Sample

    public Sample toString(int[] position, int xOffset, int yOffset) {
        String postn = String.format("[%d,%d]", position[0], position[1]);
        String movm = String.format("[%d,%d]", xOffset, yOffset);
        Sample obj = new Sample();
        obj.setPostn(postn);
        obj.setMovm(movm);
        return obj;
    }
    

    2. Using an array

    Change the method to return String array and store the values in that array:

    public String[] toString(int[] position, int xOffset, int yOffset) {
        String postn = String.format("[%d,%d]", position[0], position[1]);
        String movm = String.format("[%d,%d]", xOffset, yOffset);
        String arr[] = new String[2];
        arr[0] = postn;
        arr[1] = movm;
        return arr;
    }
    

    3. Using javatuples

    You can read details at link http://www.javatuples.org/

    4. By delimiting Data

    You can use some delimiter if you are sure about the data in the variables. But this will require one spiting with the delimiter, when you want to use these values.

    public String toString(int[] position, int xOffset, int yOffset) {
        String postn = String.format("[%d,%d]", position[0], position[1]);
        String movm = String.format("[%d,%d]", xOffset, yOffset);
        return postn+"~"+movm;
    }
    
    0 讨论(0)
  • 2021-02-08 05:15

    You cannot return two different values in methods in Java.

    When this need occurs, you usually have two options:

    1. Create a class which has the types you want to return, and then return an instance of that class. Usually these classes are very basic, contain public members and optionally a constructor to initiate an instance with all the required values.
    2. Add a reference type object as a parameter to the method which the method will mutate with the values you want to return. In your example you use strings, and so this will not work since in Java strings are immutable.

    Another option would be to use an array of strings, since both your return types are strings. You could use this option as a return value, or a parameter which is altered by the method.

    Edit: usually "toString" methods do return only one string. Perhaps you should consider concatenating both your strings into one string using some separator. Example:

    return postn + "_" + movm;
    
    0 讨论(0)
  • 2021-02-08 05:15

    In Java, when you want a function to return multiple values, you must

    • embed those values in an object you return
    • or change an object that is passed to your function ~ dystroy

    You have two options (that I know of) here:

    Option 1: Return as a new array:

     public String[] toString(int[] position,int xOffset,int yOffset) {
            String postn=String.format("[%d,%d]",position[0],position[1]);
            String movm=String.format("[%d,%d]",xOffset,yOffset);
    
            string [] myArray = new string[2];
            myArray[0] = postn;
            myArray[1] = movm;
    
            return myarray; //returns as array
         }
    

    Option 2: Return as such

    Edited to show get set

        private String a;
        private String b;
    
        public void setPostN(String s)
        {
                a= s;
        }
    
        public String getPostN()
        {
                return a;
        }
    
        public void setMovm(String s)
        {
                a= s;
        }
    
        public String getMovm() 
        {
                return a;
        }
    

    with your method:

    public void toString(int[] position,int xOffset,int yOffset) {
            String postn=String.format("[%d,%d]",position[0],position[1]);
            String movm=String.format("[%d,%d]",xOffset,yOffset);
    
            setPostN(postn);
            setMovm(movm);
    
           //no return, but available via calling getMovm() or getPostN()
         }
    
    0 讨论(0)
  • 2021-02-08 05:22

    you can either create a wrapper type, instantiate and return it, or return a Map or List/Array.

    0 讨论(0)
提交回复
热议问题