First, scene analysis:
Multiple objects can be returned only once for a method call. The workaround is to create an object that holds multiple objects that you want to return. Of course, you can create a class to do this every time you need it. A generic tool class is written here to solve the problem at once.
The concept of returning multiple objects in an object is called a tuple (tuple), which is a single object that is packaged directly into a set of objects. This container object allows you to read only the elements, not the other objects.
In general, tuples can have arbitrary lengths, while objects in tuples can be of any different type.
Second, the code implementation is as follows:
(1) The structure of the tool class is as follows:
We create a new package package tuple under the tool class Utils, and then, in which the 2-D-Tuple twotuple class, 3-D-Tuple Threetuple class, 4-D-Tuple fourtuple class, 5-D-tuple fivetuple, There is also a test class tupletest.
(2) The specific code is as follows: The 2-D-Tuple class Twotuple.java code is as follows:
public class Twotuple<a, b> {public
final A-i;
Public final B second;
Public Twotuple (a A, b b) {
this.first = A;
This.second = b;
}
Public String toString () {return
"(" + A "," + Second + ");
The 3-D-Tuple class Threetuple.java code is as follows:
public class Threetuple<a, B, c> extends Twotuple<a, b> {public
final C third;
Public Threetuple (A, B Second, C third) {
super (A, second); This should be written first
this.third = third;
}
Public String toString () {return
"(" + A + "," + Second + "," + Third + ")";
}
}
The 4-D-Tuple class Fourtuple.java code is as follows:
public class Fourtuple<a, B, C, d> extends Threetuple<a, B, c> {public
final D fourth;
Public Fourtuple (A, B Second, C third, D fourth) {
super (A, second, third);
This.fourth = Fourth;
}
Public String toString () {return
"(" + A "," + Second + "," + Third + "," + Fourth + ")";
}
}
The 5-D-Tuple class Fivetuple.java code is as follows:
public class Fivetuple<a, B, C, D, e> extends Fourtuple<a, B, C, d> {public
final E fifth;
Public Fivetuple (A, B Second, C third, D fourth, E fifth) {super (A, second
, third, fourth);
This.fifth = fifth;
}
Public String toString () {return
"(" + A + "," + Second + "," + Third + "," + Fourth + "," + Fifth + ")";
}
}
The Tupletest.java class for the test code written for the above tuples is as follows:
public class Tupletest {static twotuple<string, integer> F () {return new twotuple<
String, integer> ("Hi", 47); Static threetuple<string, Integer, Character> g () {return new threetuple<string, Integer, Character
> (New String ("Tim"), "C"); Static fourtuple<string, long, Integer, integer> h () {return new fourtuple<string, Long, Integer, I
nteger> ("Hello", 13L, 33, 2); Static fivetuple<string, long, long, String, String> K () {return new fivetuple<string, long, long,
String, string> ("Tim11", 34L, 33L, "DD", "ee"); public static void Main (string[] args) {twotuple<string, integer> Ttsi = f ();//Ttsi.first = "There";
Compile-time Run-time error: The final System.out.println (g ()) is declared as public, although it is stated as public;
System.out.println (H ());
System.out.println (K ()); }
}
Analysis:public final A; Such a declaration although the field is declared public, but because the declaration is final, so the external can not change it, if you set a new value, at compile time will be an error.