如何用Java返回多个值?

2021年3月17日15:18:06 发表评论 1,994 次浏览

Java不支持多值返回。我们可以使用以下解决方案返回多个值。

如果所有返回的元素都属于同一类型

我们可以用Java返回一个数组。下面是一个演示相同内容的Java程序。

// A Java program to demonstrate that a method
// can return multiple values of same type by
// returning an array
class Test {
     // Returns an array such that first element
     // of array is a+b, and second element is a-b
     static int [] getSumAndSub( int a, int b)
     {
         int [] ans = new int [ 2 ];
         ans[ 0 ] = a + b;
         ans[ 1 ] = a - b;
  
         // returning array of elements
         return ans;
     }
  
     // Driver method
     public static void main(String[] args)
     {
         int [] ans = getSumAndSub( 100 , 50 );
         System.out.println( "Sum = " + ans[ 0 ]);
         System.out.println( "Sub = " + ans[ 1 ]);
     }
}

输出如下:

Sum = 150
Sub = 50

如果返回的元素属于不同类型

使用对(如果只有两个返回值)

我们可以用

用Java配对

返回两个值。

// Returning a pair of values from a function
import javafx.util.Pair;
  
class GfG {
     public static Pair<Integer, String> getTwo()
     {
         return new Pair<Integer, String>( 10 , "lsbin" );
     }
  
     // Return multiple values from a method in Java 8
     public static void main(String[] args)
     {
         Pair<Integer, String> p = getTwo();
         System.out.println(p.getKey() + " " + p.getValue());
     }
}

如果有两个以上的返回值

我们可以将所有返回的类型封装到一个类中, 然后返回该类的一个对象。

让我们看看下面的代码。

// A Java program to demonstrate that we can return
// multiple values of different types by making a class
// and returning an object of class.
  
// A class that is used to store and return
// three members of different types
class MultiDivAdd {
     int mul; // To store multiplication
     double div; // To store division
     int add; // To store addition
     MultiDivAdd( int m, double d, int a)
     {
         mul = m;
         div = d;
         add = a;
     }
}
  
class Test {
     static MultiDivAdd getMultDivAdd( int a, int b)
     {
         // Returning multiple values of different
         // types by returning an object
         return new MultiDivAdd(a * b, ( double )a / b, (a + b));
     }
  
     // Driver code
     public static void main(String[] args)
     {
         MultiDivAdd ans = getMultDivAdd( 10 , 20 );
         System.out.println( "Multiplication = " + ans.mul);
         System.out.println( "Division = " + ans.div);
         System.out.println( "Addition = " + ans.add);
     }
}

输出如下:

Multiplication = 200
Division = 0.5
Addition = 30

返回清单对象类别

// Java program to demonstrate return of
// multiple values from a function using
// list Object class.
import java.util.*;
  
class GfG {
     public static List<Object> getDetails()
     {
         String name = "Geek" ;
         int age = 35 ;
         char gender = 'M' ;
  
         return Arrays.asList(name, age, gender);
     }
  
     // Driver code
     public static void main(String[] args)
     {
         List<Object> person = getDetails();
         System.out.println(person);
     }
}

输出如下:

[Geek, 35, M]

本文作者:一闪八木。如果你喜欢lsbin并希望做出贡献, 那么你也可以写一篇文章并将你的文章邮寄到contribution@lsbin.org。查看你的文章出现在lsbin主页上, 并帮助其他Geeks。

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: