int[]、float[]或double[]数组的Java函数
我想编写一个Java函数,它将作为输入获取int[]、float[]或double[]数组的Java函数,java,arrays,Java,Arrays,我想编写一个Java函数,它将作为输入获取int[],float[]或double[]。算法完全相同(某种标量积)。如何编写能够处理所有类型数字数组的单个函数?我看到两个选项: 1) 您可以创建一个新类,该类允许构造函数中的int[],float[],double[],并保存它们 2) 允许对象[]并检查int/float/double。(必须先转换它们)使用java.lang.Number类型或对象参数类型 要了解更多信息,请阅读在Java中没有简单的方法来处理此问题。你可以: 使用包装类型
int[]
,float[]
或double[]
。算法完全相同(某种标量积)。如何编写能够处理所有类型数字数组的单个函数?我看到两个选项:
1) 您可以创建一个新类,该类允许构造函数中的int[],float[],double[],并保存它们
2) 允许对象[]并检查int/float/double。(必须先转换它们)使用
java.lang.Number
类型或对象
参数类型
要了解更多信息,请阅读在Java中没有简单的方法来处理此问题。你可以:
- 使用包装类型(
,Integer[]
,Float[]
)并具有一个以Double[]
为参数的函数。因为数组在Java中是协变的,所以可以工作:Number[]
请注意,这种方法会显著增加内存消耗
- 将
和int[]
数组转换为float[]
并始终使用double。最好创建方法的重载版本,其中采用double[]
和int[]
的方法只进行转换并委托给实际的float[]
实现double[]
- 我相信Scala可以无缝地处理这个问题,因为Java基本类型在Scala中是语义对象
- 分别对每个案例进行编码,或
- 正在对阵列上的所有操作使用反射。。。这可能是混乱的、脆弱的,并且比最优解决方案慢一个数量级
public static void main(String... args) throws IOException {
int[] nums = new int[10*1000 * 1000];
{
long start = System.nanoTime();
product2(nums);
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to take the product of %,d ints using an int[].%n", time / 1e9, nums.length);
}
{
long start = System.nanoTime();
product(nums);
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to take the product of %,d ints using reflections.%n", time / 1e9, nums.length);
}
}
public static double product(Object array) {
double product = 1;
for (int i = 0, n = Array.getLength(array); i < n; i++)
product *= ((Number) Array.get(array, i)).doubleValue();
return product;
}
public static double product2(int... nums) {
double product = 1;
for (int i = 0, n = nums.length; i < n; i++)
product *= nums[i];
return product;
}
int[]
float[]
和double[]
的唯一常见超类型是Object
,因此不可能在这些类型上使用多态性。同样,泛型要求类型参数是引用类型,int
、float
和double
不是引用类型
您需要接受将有重复的代码,或者更改数组的表示类型;e、 g.使用
Integer[]
/Float[]
/Double[]
或Number[]
您可以编写一个方法来完成所有这些操作,但是,它的可读性和效率远远不如前者。您必须在通用解决方案和有效解决方案之间做出选择
public static void main(String... args) throws IOException {
int[] nums = new int[10*1000 * 1000];
{
long start = System.nanoTime();
product2(nums);
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to take the product of %,d ints using an int[].%n", time / 1e9, nums.length);
}
{
long start = System.nanoTime();
product(nums);
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to take the product of %,d ints using reflections.%n", time / 1e9, nums.length);
}
}
public static double product(Object array) {
double product = 1;
for (int i = 0, n = Array.getLength(array); i < n; i++)
product *= ((Number) Array.get(array, i)).doubleValue();
return product;
}
public static double product2(int... nums) {
double product = 1;
for (int i = 0, n = nums.length; i < n; i++)
product *= nums[i];
return product;
}
如果您只处理相对较小的阵列,则通用但效率较低的解决方案可能足够快。class ArrayMath{
class ArrayMath {
private ArrayMath() {
}
public static <T extends Number> double ScalarProduct(T[] a, T[] b){
double sum = 0;
for(int i=0;i<a.length;++i){
sum += a[i].doubleValue()*b[i].doubleValue();
}
return sum;
}
}
class Sample {
public static void main(String arg[]){
Integer[] v1 = { 1, -10, 3, 9, 7, 99, -25 };
Integer[] v2 = { 1, -10, 3, 9, 7, 99, -25 };
double p_int = ArrayMath.ScalarProduct(v1, v2);
Double[] v1_d = { 1.1, -10.5, 3.7, 9.98, 7.4, 9.9, -2.5 };
Double[] v2_d = { 1.1, -10.5, 3.7, 9.98, 7.4, 9.9, -2.5 };
Double p_double = ArrayMath.ScalarProduct(v1_d, v2_d);
System.out.println("p_int:" + p_int);
System.out.println("p_double:" + p_double);
}
}
private ArrayMath(){
}
公共静态双标度积(T[]a,T[]b){
双和=0;
对于(int i=0;i希望这将对您有所帮助..我已经测试了这段代码,它满足了您的要求,如何实现这一逻辑取决于您
public class MultiDataType {
public static void main(String[] args) {
int[] i = new int[2];
float[] f = new float[2];
double[] d = new double[2];
String str = new String();
handlingFunction(i);
handlingFunction(f);
handlingFunction(d);
handlingFunction(str);
}
public static void handlingFunction(Object o) {
String classType = null;
if (o.getClass().getCanonicalName().equals("int[]")) {
classType = "int[]";// Your handling code goes here
} else if (o.getClass().getCanonicalName().equals("float[]")) {
classType = "float[]";// Your handling code goes here
} else if (o.getClass().getCanonicalName().equals("double[]")) {
classType = "double[]";// Your handling code goes here
}else classType = o.getClass().getCanonicalName();
System.out.println("Object belongs to " + classType);
}
}
输出
Object belongs to int[]
Object belongs to float[]
Object belongs to double[]
Object belongs to java.lang.String
泛型对基类型为基类型的数组没有帮助。不幸的是,你不能将两个数字相乘。@aioobe:嗯,我不能做number1*number2
,但我可以number1.doubleValue()*number2.doubleValue()
…你不能将int[]
(或float[]
或double[]
)作为对象[]
。这些类型在Java中是不兼容的。是的,你必须先转换它们。你想在单个函数中处理什么?你能给出一些例子吗?谢谢,但这不是我的意思-我想为所有类型的数组执行相同的操作,而你的函数执行不同的操作(除非我实现同样的事情3次)。Java不是魔法…所以在某个地方或其他地方,你必须做一些编码…如果你对你想要的相同操作有点具体,那么我可以帮你!+1对于解决方案,但是,它适用于装箱版本(仅限整数、浮点和双数组),而我需要int、float和double数组。
Object belongs to int[]
Object belongs to float[]
Object belongs to double[]
Object belongs to java.lang.String