我有这个方法:
public static long[] computeDifferenceArray(long[] array) {
long[] diffArray = new long[array.length - 1];
for (int i = 0; i < diffArray.length; i++) {
diffArray[i] = array[i + 1] - array[i];
}
return diffArray;
}目前,它只接受long[]并返回一个long[]。但我也想在int[]上使用同样的方法。我该怎么做呢?
我读到过Java泛型只适用于类对象。
发布于 2020-04-14 15:08:46
你必须在每次你的int[]到long[].You的时候进行转换。
公共静态空main(String[]参数){
int[] a1=new int[] {1,2,3,4,5};
Test1.computeDifferenceArray( Arrays.stream(a1).mapToLong(i -> i).toArray());
System.out.println("56897");
}
public static Long[] computeDifferenceArray(long[] array) {
Long[] diffArray = new Long[array.length - 1];
for (int i = 0; i < diffArray.length; i++) {
diffArray[i] = array[i + 1] - array[i];
}
return diffArray;
}发布于 2020-04-16 02:08:09
实现您需求的最好方法是编写一个泛型方法,并注入要应用于数组成员的所需行为-如果您愿意,这是Strategy模式的一个实现。随着streams的引入,你可以很优雅地做到这一点,请看你的代码以给定的方式重写,如下所示:
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import static junit.framework.TestCase.assertEquals;
// The logic context
class ArrayDifference {
public <T> List<T> difStream(T[] input, BiFunction<T,T,T> operation) {
List<T> output = new ArrayList<>();
for (int i=0; i < input.length-1; i++) {
output.add(operation.apply(input[i], input[i+1]));
}
return output;
}
}
// additional reducing functions
class Reducers {
static float substract(float f1, float f2) {
return f1-f2;
}
static double substract(double d1, double d2) {
return d1-d2;
}
}
public class ArrayDifferenceTest {
ArrayDifference arraySubstracter = new ArrayDifference();
@Test
public void test_integer() {
Integer[] input = new Integer[]{Integer.valueOf(1),
Integer.valueOf(2), Integer.valueOf(3)};
List<Integer> expectedResult = Arrays.asList(-1,-1);
// here you can see how it is called for integer
List<Integer> result = arraySubstracter.difStream(input, Math::subtractExact);
assertEquals(expectedResult, result);
}
@Test
public void test_long() {
Long[] input = new Long[]{Long.valueOf(1), Long.valueOf(2), Long.valueOf(3)};
List<Long> expectedResult = Arrays.asList(-1L,-1L);;
// here you can see how it is called for long
List<Long> result = arraySubstracter.difStream(input, Math::subtractExact);
assertEquals(expectedResult, result);
}
@Test
public void test_double() {
Double[] input = new Double[]{Double.valueOf(1), Double.valueOf(2), Double.valueOf(3)}
List<Double> expectedResult = Arrays.asList(-1D,-1D);
// here you can see how it is called for double
List<Double> result = arraySubstracter.difStream(input, Reducers::substract);
assertEquals(expectedResult, result);
}
@Test
public void test_float() {
Float[] input = {Float.valueOf(1F), Float.valueOf(2F), Float.valueOf(3F)};
List<Float> expectedResult = Arrays.asList(-1F, -1F);
// here you can see how it is called for float
List<Float> result = arraySubstracter.difStream(input, Reducers::substract);
assertEquals(expectedResult, result);
}
}可以看到,方法是不变的,可以从外部注入行为。出于测试的目的,我使用了Math.substractExact()方法,而对于float和double,我编写了简单的方法。通过这种方式,代码是可扩展的,并且很容易在以后添加新的数据类型。
https://stackoverflow.com/questions/61201604
复制相似问题