JAVA : 如何将数组中同一索引处的值求和到一个数组中
JAVA : How to sum values at the same index in arrays into a single array
如何将数组中同一索引处的值相加到一个数组中?
我们有一个数组数组,需要编写一个函数接受这个数组和 returns 一个表示原始数组对应元素之和的新数组。
若原数组为-
[
[43, 2, 21],[1, 2, 4, 54],[5, 84, 2],[11, 5, 3, 1]
]
那么输出应该是-
[60, 93, 30, 55]
我想用 JAVA
得到这个结果
我通过谷歌搜索找到了 javascript 代码。
谢谢:)
你可以用下面的代码来实现-
const arr = [[43, 2, 21],[1, 2, 4, 54],[5, 84, 2],[11, 5, 3, 1]];
const sumArray = (array) => {
const newArray = [];
array.forEach(sub => {
sub.forEach((num, index) => {
if(newArray[index]){
newArray[index] += num;
}else{
newArray[index] = num;
}
});
});
return newArray;
}
public static void main(String args[]) {
int numbers[][] = {{43, 2, 21},{1, 2, 4, 54},{5, 84, 2},{11, 5, 3, 1}};
int result[]=new int[4];
for(int a=0;a<4;a++) {
for(int b=0;b<4;b++) {
try {
result[b]+=numbers[a][b];
}catch(IndexOutOfBoundsException e) {
}
}
}
System.out.println(Arrays.toString(result));
}
这个解决方案可以正常工作,只是需要 O(n^2) 才能完成。对于数组元素大小不相同的情况,我们可以记录一些错误消息。
public static void main (String[] args) {
int[][] input = {{43, 2, 21},{1, 2, 4, 54},{5, 84, 2},{11, 5, 3, 1}};
int[] output = new int[input.length];
for (int i = 0; i < input.length ; i++) {
for (int j = 0; j < input.length; j++) {
try {
output[j] = output[j] + input[i][j];
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Size is not same");
}
}
}
System.out.println(Arrays.toString(output));
}
如果你知道 'Two Pointer Algorithm',你可以在 O(n) 时间复杂度内解决这个问题。
public static void main(String[] args) {
int[][] num = {{43, 2, 21, 100},{1, 2, 4, 54},{5,84,2,1,1},{11,5,3,1}};
int size = 0;
int i = 0;
int j = 0;
int sum = 0;
while(true){
if(i == num.length){
/**
* insert sum into result array here
*/
if(i == num.length && j == num[i-1].length)
break;
sum = 0;
i = 0;
j++;
}
if(j < num[i].length) {
sum += num[i][j];
}
i++;
}
}
lambda 始终是简短解决方案的不错选择
int mat[][] = { {43, 2, 21}, {1, 2, 4, 54}, {5, 84, 2}, {11, 5, 3, 1}};
int[] array = Arrays.stream(mat)
.reduce((a1, a2) -> IntStream.range(0, Math.max(a1.length, a2.length))
.map(i -> i < a1.length && i < a2.length ? a1[i] + a2[i]
: i < a1.length ? a1[i] : a2[i] ).toArray()).get();
(可以处理不同长度的行)
如何将数组中同一索引处的值相加到一个数组中?
我们有一个数组数组,需要编写一个函数接受这个数组和 returns 一个表示原始数组对应元素之和的新数组。
若原数组为-
[
[43, 2, 21],[1, 2, 4, 54],[5, 84, 2],[11, 5, 3, 1]
]
那么输出应该是-
[60, 93, 30, 55]
我想用 JAVA
得到这个结果我通过谷歌搜索找到了 javascript 代码。
谢谢:)
你可以用下面的代码来实现-
const arr = [[43, 2, 21],[1, 2, 4, 54],[5, 84, 2],[11, 5, 3, 1]];
const sumArray = (array) => {
const newArray = [];
array.forEach(sub => {
sub.forEach((num, index) => {
if(newArray[index]){
newArray[index] += num;
}else{
newArray[index] = num;
}
});
});
return newArray;
}
public static void main(String args[]) {
int numbers[][] = {{43, 2, 21},{1, 2, 4, 54},{5, 84, 2},{11, 5, 3, 1}};
int result[]=new int[4];
for(int a=0;a<4;a++) {
for(int b=0;b<4;b++) {
try {
result[b]+=numbers[a][b];
}catch(IndexOutOfBoundsException e) {
}
}
}
System.out.println(Arrays.toString(result));
}
这个解决方案可以正常工作,只是需要 O(n^2) 才能完成。对于数组元素大小不相同的情况,我们可以记录一些错误消息。
public static void main (String[] args) {
int[][] input = {{43, 2, 21},{1, 2, 4, 54},{5, 84, 2},{11, 5, 3, 1}};
int[] output = new int[input.length];
for (int i = 0; i < input.length ; i++) {
for (int j = 0; j < input.length; j++) {
try {
output[j] = output[j] + input[i][j];
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Size is not same");
}
}
}
System.out.println(Arrays.toString(output));
}
如果你知道 'Two Pointer Algorithm',你可以在 O(n) 时间复杂度内解决这个问题。
public static void main(String[] args) {
int[][] num = {{43, 2, 21, 100},{1, 2, 4, 54},{5,84,2,1,1},{11,5,3,1}};
int size = 0;
int i = 0;
int j = 0;
int sum = 0;
while(true){
if(i == num.length){
/**
* insert sum into result array here
*/
if(i == num.length && j == num[i-1].length)
break;
sum = 0;
i = 0;
j++;
}
if(j < num[i].length) {
sum += num[i][j];
}
i++;
}
}
lambda 始终是简短解决方案的不错选择
int mat[][] = { {43, 2, 21}, {1, 2, 4, 54}, {5, 84, 2}, {11, 5, 3, 1}};
int[] array = Arrays.stream(mat)
.reduce((a1, a2) -> IntStream.range(0, Math.max(a1.length, a2.length))
.map(i -> i < a1.length && i < a2.length ? a1[i] + a2[i]
: i < a1.length ? a1[i] : a2[i] ).toArray()).get();
(可以处理不同长度的行)