gpt4 book ai didi

java - Java中如何合并两个数组?

转载 作者:行者123 更新时间:2023-12-03 01:53:58 24 4
gpt4 key购买 nike

它不是连接,而是合并两个数组,使它们成为名称值对的数组。

firstarray = ["a","aa","aaa"]
secondarray = ["b","bb","bbb"]
result = [["a","b"],["aa","bb"],["aaa","bbb"]]

最好的方法是什么?

最佳答案

在 Java 中:

public static int [][] concat(int a[], int b[]) {
int res[][] = new int[a.length][2];
if (a.length != b.length) {
throw new IllegalArgumentException("lenght are not equal, cannot perform");
}
for (int i = 0; i < a.length; i++) {
res[i][0] = a[i];
res[i][1] = b[i];
}
return res;
}

关于java - Java中如何合并两个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4459258/

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