Java 从一个方法返回两个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26585709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return two variables from one method
提问by Lbert Hartanto
How to write the following code correctly?
如何正确编写以下代码?
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);
}
Following error came up
出现以下错误
movm cannot be returned.
回答by ifloop
When using Java 8you could make use of the Pairclass.
使用Java 8 时,您可以使用Pair类。
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.
否则只需返回一个数组。
回答by Kent
you can either create a wrapper type, instantiate and return it, or return a Map or List/Array.
您可以创建一个包装器类型,实例化并返回它,或者返回一个 Map 或 List/Array。
回答by Gil Moshayof
You cannot return two different values in methods in Java.
您不能在 Java 的方法中返回两个不同的值。
When this need occurs, you usually have two options:
当出现这种需求时,您通常有两种选择:
- 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.
- 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.
- 创建一个具有要返回的类型的类,然后返回该类的实例。通常这些类是非常基本的,包含公共成员和可选的构造函数来启动具有所有必需值的实例。
- 将引用类型对象作为参数添加到方法中,该方法将使用您要返回的值进行变异。在您的示例中,您使用字符串,因此这将不起作用,因为在 Java 中字符串是不可变的。
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:
编辑:通常“toString”方法只返回一个字符串。也许您应该考虑使用一些分隔符将两个字符串连接成一个字符串。例子:
return postn + "_" + movm;
回答by Gil Moshayof
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
在Java中,当你想让一个函数返回多个值时,你必须
- 将这些值嵌入您返回的对象中
- 或更改传递给您的函数的对象〜dystroy
You have two options (that I know of) here:
你在这里有两个选择(我知道):
Option 1: Return as a new array:
选项 1:作为新数组返回:
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
选项 2:原样返回
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()
}
回答by Ashu
There are four method which I can think of to implement this scenario:
我可以想到四种方法来实现这个场景:
1. Using a class
1. 使用类
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
并更改您的方法以包含此类的对象并在这些属性中设置值。将方法的返回类型更改为类名,例如 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
2. 使用数组
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
3. 使用 javatuples
You can read details at link http://www.javatuples.org/
您可以在链接http://www.javatuples.org/阅读详细信息
4. By delimiting Data
4. 通过定界数据
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;
}