gpt4 book ai didi

java - 如何从同一个java方法返回两个多维数组?

转载 作者:搜寻专家 更新时间:2023-11-01 01:12:46 25 4
gpt4 key购买 nike

我想知道如何从同一个方法返回两个多维数组:

public static arraysReturn() {
int [][] A={{1,2},{2,3},{4,5}};
int [][] B={{1,2},{2,3},{4,5}};
return A,B;
}

最佳答案

Java 不支持一次返回多个东西。

但是,您可以创建一个小类来执行此操作:

public class TwoArrays {
public final int[][] A;
public final int[][] B;
public TwoArrays(int[][] A, int[][] B) {
this.A = A;
this.B = B;
}
}

然后把你的方法变成这样:

public static TwoArrays arraysreturn() {
int [][] A={{1,2},{2,3},{4,5}};
int [][] B={{1,2},{2,3},{4,5}};
return new TwoArrays(A,B);
}

访问值:

TwoArrays arrays = arraysreturn();
System.out.println(Arrays.toString(arrays.A)); //Due to the way java prints arrays, this is needed, but it isn't a requirement for doing other stuff with the array.
System.out.println(Arrays.toString(arrays.B));

关于java - 如何从同一个java方法返回两个多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com