/** 时代Java - nowjava.com 提 供 **/
//package com.nowjava;
import java.util.Arrays;
public class Main {
public static void main(String[] argv) throws Exception {
int[] first = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
int[] second = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
System.out.println(java.util.Arrays.toString(add(first, second)));
}
private static int[] EMPTY_INT_ARRAY = new int[0];
private static float[] EMPTY_FLOAT_ARRAY = new float[0];
/**
* Adds the values from the second array to the first,
* reusing the first array if possible
* The result will be expanded to fit the maximum number of array elements in either array
*/
public static int[] add(int[] first, int[] second) {
if (first == null && second == null)
return EMPTY_INT_ARRAY;
if (second == null)
return first;
if (first == null)
/*
来自
*时代Java公众号 - N o w J a v a . c o m*/
return Arrays.copyOf(second, second.length);
if (first.length < second.length) {
first = Arrays.copyOf(first, second.length);
}
int nOverlap = Math.min(first.length, second.length);
for (int i = 0; i < nOverlap; i++) {
first[i] += second[i];
}
return first;
}
/**
* Adds the values from the second array to the first,
* reusing the first array if possible
* The result will be expanded to fit the maximum number of array elements in either array
*/
public static float[] add(float[] first, float[] second) {
if (first == null && second == null)
return EMPTY_FLOAT_ARRAY;
if (second == null)
return first;
if (first == null)
return Arrays.copyOf(second, second.length);
if (first.length < second.length) {
first = Arrays.copyOf(first, second.length);
}
int nOverlap = Math.min(first.length, second.length);
for (int i = 0; i < nOverlap; i++) {
first[i] += second[i];
}
return first;
}
/**
* Adds the given value to the given position in the array, copying and padding with zeroes
* is required.
* @param array
* @param position
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。