java元组里面套元组
时间: 2024-05-28 12:08:18 浏览: 212
Java中没有元组(tuple)的概念,但是可以通过使用嵌套类或者数组来实现类似于元组的功能。在Java中嵌套类可以嵌套多层,因此可以创建一个类似于元组里面套元组的结构。举个例子,以下代码实现了一个三元组套四元组的结构:
```
public class TupleExample {
public static void main(String[] args) {
Tuple3<Tuple4<Integer, Integer, Integer, Integer>, Tuple4<Integer, Integer, Integer, Integer>, Tuple4<Integer, Integer, Integer, Integer>> tuple = new Tuple3<>(
new Tuple4<>(1, 2, 3, 4),
new Tuple4<>(5, 6, 7, 8),
new Tuple4<>(9, 10, 11, 12)
);
System.out.println(tuple);
}
}
class Tuple3<A, B, C> {
public final A a;
public final B b;
public final C c;
public Tuple3(A a, B b, C c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString() {
return "(" + a.toString() + ", " + b.toString() + ", " + c.toString() + ")";
}
}
class Tuple4<A, B, C, D> {
public final A a;
public final B b;
public final C c;
public final D d;
public Tuple4(A a, B b, C c, D d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
@Override
public String toString() {
return "(" + a.
阅读全文