jav*_*ava 168 java function return-value
我试图从Java方法返回2个值但我得到这些错误.这是我的代码:
// Method code
public static int something(){
int number1 = 1;
int number2 = 2;
return number1, number2;
}
// Main method code
public static void main(String[] args) {
something();
System.out.println(number1 + number2);
}
Run Code Online (Sandbox Code Playgroud)
错误:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at assignment.Main.something(Main.java:86)
at assignment.Main.main(Main.java:53)
Run Code Online (Sandbox Code Playgroud)
Java结果:1
Jes*_*per 224
不要返回包含这两个值或使用泛型Pair类的数组,而应考虑创建一个表示要返回的结果的类,并返回该类的实例.给这个班一个有意义的名字.与使用阵列相比,这种方法的好处是类型安全,它将使您的程序更容易理解.
注意:Pair这里的一些其他答案中提出的泛型类也为您提供类型安全性,但不传达结果所代表的含义.
示例(不使用真正有意义的名称):
final class MyResult {
private final int first;
private final int second;
public MyResult(int first, int second) {
this.first = first;
this.second = second;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
// ...
public static MyResult something() {
int number1 = 1;
int number2 = 2;
return new MyResult(number1, number2);
}
public static void main(String[] args) {
MyResult result = something();
System.out.println(result.getFirst() + result.getSecond());
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 67
Java不支持多值返回.返回一组值.
// Function code
public static int[] something(){
int number1 = 1;
int number2 = 2;
return new int[] {number1, number2};
}
// Main class code
public static void main(String[] args) {
int result[] = something();
System.out.println(result[0] + result[1]);
}
Run Code Online (Sandbox Code Playgroud)
Lar*_*ren 39
Pair如果您确定只需要返回两个值,则可以实现泛型:
public class Pair<U, V> {
/**
* The first element of this <code>Pair</code>
*/
private U first;
/**
* The second element of this <code>Pair</code>
*/
private V second;
/**
* Constructs a new <code>Pair</code> with the given values.
*
* @param first the first element
* @param second the second element
*/
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
//getter for first and second
Run Code Online (Sandbox Code Playgroud)
然后让方法返回Pair:
public Pair<Object, Object> getSomePair();
Run Code Online (Sandbox Code Playgroud)
ric*_*chj 27
你只能在Java中返回一个值,所以最好的方法是这样的:
return new Pair<Integer>(number1, number2);
Run Code Online (Sandbox Code Playgroud)
这是您的代码的更新版本:
public class Scratch
{
// Function code
public static Pair<Integer> something() {
int number1 = 1;
int number2 = 2;
return new Pair<Integer>(number1, number2);
}
// Main class code
public static void main(String[] args) {
Pair<Integer> pair = something();
System.out.println(pair.first() + pair.second());
}
}
class Pair<T> {
private final T m_first;
private final T m_second;
public Pair(T first, T second) {
m_first = first;
m_second = second;
}
public T first() {
return m_first;
}
public T second() {
return m_second;
}
}
Run Code Online (Sandbox Code Playgroud)
这是使用SimpleEntry真正真正简单的解决方案:
AbstractMap.Entry<String, Float> myTwoCents=new AbstractMap.SimpleEntry<>("maximum possible performance reached" , 99.9f);
String question=myTwoCents.getKey();
Float answer=myTwoCents.getValue();
Run Code Online (Sandbox Code Playgroud)
仅使用Java内置函数,并且具有类型安全性好处。
你必须使用集合来返回多个返回值
在你的情况下,你编写代码为
public static List something(){
List<Integer> list = new ArrayList<Integer>();
int number1 = 1;
int number2 = 2;
list.add(number1);
list.add(number2);
return list;
}
// Main class code
public static void main(String[] args) {
something();
List<Integer> numList = something();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
456923 次 |
| 最近记录: |