两数组相加java_Java 将两个整数数组元素中的值相加

//package com.nowjava;

/*时代Java - N o w J a v a . c o m 提 供*/

public class Main {

public static void main(String[] argv) throws Exception {

int[] a = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };

int[] b = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };

System.out.println(java.util.Arrays.toString(addElementwise(a, b)));

}

/**

* Adds up the values in the two arrays of integers element-wise.

*

* @param a Array of integers.

* @param b Array of integers.

* @return Element-wise sum of arrays.

* @throws ArithmeticException Throws an exception when the arrays differ in

* length.

*/

public static int[] addElementwise(int[] a, int[] b)

throws ArithmeticException {/** 时 代 J a v a 公 众 号 - nowjava.com 提供 **/

if (a.length != b.length) {

throw new ArithmeticException();

} else {

int[] result = new int[a.length];

for (int i = 0; i < a.length; i++) {

result[i] = a[i] + b[i];

}

return result;

}

}

/**

* Adds up the values in the two arrays of doubles element-wise.

*

* @param a Array of doubles.

* @param b Array of doubles.

* @return Element-wise sum of arrays.

* @throws ArithmeticException Throws an exception when the arrays differ in

* length.

*/

public static double[] addElementwise(double[] a, double[] b)

throws ArithmeticException {

if (a.length != b.length) {

throw new ArithmeticException();

} else {

double[] result = new double[a.length];

for (int i = 0; i < a.length; i++) {

result[i] = a[i] + b[i];

}

return result;

}

}

/**

* Adds up the values in the two arrays of longs element-wise.

*

* @param a Array of longs.

* @param b Array of longs.

* @return Element-wise sum of arrays.

* @throws ArithmeticException Throws an exception when the arrays differ in

* length.

*/

public static long[] addElementwise(long[] a, long[] b)

throws ArithmeticException {

if (a.length != b.length) {

throw new ArithmeticException();

} else {

long[] result = new long[a.length];

for (int i = 0; i < a.length; i++) {

result[i] = a[i] + b[i];

}

return result;

}

}

/**

* Adds up the values in the two arrays of floats element-wise.

*

* @param a Array of floats.

* @param b Array of floats.

* @return Element-wise sum of arrays.

* @throws ArithmeticException Throws an exception when the arrays differ in

* length.

*/

public static float[] addElementwise(float[] a, float[] b)

throws ArithmeticException {

if (a.length != b.length) {

throw new ArithmeticException();

} else {

float[] result = new float[a.length];

for (int i = 0; i < a.length; i++) {

result[i] = a[i] + b[i];

}

return result;

}

}

/**

* Adds up the values in the two arrays of bytes element-wise.

*

* @param a Array of bytes.

* @param b Array of bytes.

* @return Element-wise sum of arrays.

* @throws ArithmeticException Throws an exception when the arrays differ in

* length.

*/

public static byte[] addElementwise(byte[] a, byte[] b)

throws ArithmeticException {

if (a.length != b.length) {

throw new ArithmeticException();

} else {

byte[] result = new byte[a.length];

for (int i = 0; i < a.length; i++) {

result[i] = (byte) (a[i] + b[i]);

}

return result;

}

}

/**

* Adds up the values in an array of doubles and an array of integers

* element-wise.

*

* @param a Array of doubles.

* @param b Array of integers.

* @return Element-wise sum of arrays.

* @throws ArithmeticException Throws an exception when the arrays differ in

* length.

*/

public static double[] addElementwise(double[] a, int[] b)

throws ArithmeticException {

if (a.length != b.length) {

throw new ArithmeticException();

} else {

double[] result = new double[a.length];

for (int i = 0; i < a.length; i++) {

result[i] = a[i] + b[i];

}

return result;

}

}

/**

* Adds up two matrices of doubles element-wise.

*

* @param a Matrix of doubles.

* @param b Matrix of doubles.

* @return Element-wise sum of the two matrices.

* @throws ArithmeticException Throws an exception when the two matrices are

* not of the same size.

*/

public static double[][] addElementwise(double[][] a, double[][] b)

throws ArithmeticException {

if (a.length != b.length || a[0].length != b[0].length) {

throw new ArithmeticException();

} else {

double[][] result = new double[a.length][a[0].length];

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[0].length; j++) {

result[i][j] = a[i][j] + b[i][j];

}

}

return result;

}

}

/**

* Adds up two matrices of doubles and integers element-wise.

*

* @param a Matrix of doubles.

* @param b Matrix of integers.

* @return Element-wise sum of the two matrices.

* @throws ArithmeticException Throws an exception when the two matrices are

* not of the same size.

*/

public static double[][] addElementwise(double[][] a, int[][] b)

throws ArithmeticException {

if (a.length != b.length || a[0].length != b[0].length) {

throw new ArithmeticException();

} else {

double[][] result = new double[a.length][a[0].length];

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[0].length; j++) {

result[i][j] = a[i][j] + b[i][j];

}

}

return result;

}

}

/**

* Adds up two matrices of integers element-wise.

*

* @param a Matrix of integers.

* @param b Matrix of integers.

* @return Element-wise sum of the two matrices.

* @throws ArithmeticException Throws an exception when the two matrices are

* not of the same size.

*/

public static int[][] addElementwise(int[][] a, int[][] b)

throws ArithmeticException {

if (a.length != b.length || a[0].length != b[0].length) {

throw new ArithmeticException();

} else {

int[][] result = new int[a.length][a[0].length];

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[0].length; j++) {

result[i][j] = a[i][j] + b[i][j];

}

}

return result;

}

}

/**

* Adds up two matrices of doubles element-wise.

*

* @param a Matrix of doubles.

* @param b Matrix of doubles.

* @return Element-wise sum of the two matrices.

* @throws ArithmeticException Throws an exception when the two matrices are

* not of the same size.

*/

public static float[][] addElementwise(float[][] a, float[][] b)

throws ArithmeticException {

if (a.length != b.length || a[0].length != b[0].length) {

throw new ArithmeticException();

} else {

float[][] result = new float[a.length][a[0].length];

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[0].length; j++) {

result[i][j] = a[i][j] + b[i][j];

}

}

return result;

}

}

/**

* Adds up two matrices of longs element-wise.

*

* @param a Matrix of longs.

* @param b Matrix of longs.

* @return Element-wise sum of the two matrices.

/**代码未完, 请加载全部代码(NowJava.com).**/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值