package com.itheima;
/*
将两个数组整合成一个数组,新数组的每个元素为两个数组对一个元素的和
输入: {{1,1},{2,2},{3,3}}, {{4,4},{5,5},{6,6}}
输出:{{5,5},{7,7},{9,9}}
*/
public class twoArrayAdd
{
public static void main(String[] args)
{
int[][] aTst1 = {{1,1},{2,2},{3,3}}, aTst2 = {{4,4},{5,5},{6,6}};
arraysAdd(aTst1,aTst2);
}
static void arraysAdd(int[][] a,int[][] b)
{
StringBuffer sbResult = new StringBuffer("{");
int[][] result = new int[a.length][b.length];
for(int i=0;i<a.length;++i)
{
sbResult.append("{");
for(int j=0;j<a[i].length;++j)
{
result[i][j] = a[i][j]+b[i][j];
sbResult.append(result[i][j]);
if(j == a[i].length-1)
{
if(i == a.length-1)
{
sbResult.append("}");
}
else
{
sbResult.append("},");
}
break;
}
sbResult.append(",");
}
if(i == a.length-1)
{
sbResult.append("}");
break;
}
}
System.out.println(sbResult);
}
}
|
|