[英]How can I make a method take an array of any type as a parameter?
I would like to be able to take in any array type as a parameter in a method.: 我希望能够将任何数组类型作为方法中的参数:
public void foo(Array[] array) {
System.out.println(array.length)
}
Is there a way where I could pass a String[] or int[] array, in the same method? 有没有办法在同一个方法中传递String []或int []数组?
public <T>void foo(T[] array) {
System.out.println(array.length);
}
This will not work for array of primitive types, such as int[]
, boolean[]
, double[]
,... You have to use their class wrappers instead: Integer[]
, Boolean[]
, Double[]
, ... or overload your method for each needed primitive type separately. 这对于基本类型数组不起作用,例如
int[]
, boolean[]
, double[]
,......你必须使用它们的类包装器: Integer[]
, Boolean[]
, Double[]
,.. 。或分别为每个所需的基本类型重载您的方法。
Well, you can do something like this (because an array is an Object) - 好吧,你可以这样做(因为数组是一个对象) -
public static int getArrayLength(Object array) {
return Array.getLength(array);
}
public static void main(String[] args) {
int[] intArray = { 1, 2, 3 };
String[] stringArray = { "1", "2", "c", "d" };
System.out.println(getArrayLength(intArray));
System.out.println(getArrayLength(stringArray));
}
Output is 输出是
3
4
It could be possible using Generics. 可以使用泛型。
I you don't know about it I recommend you to read about it in the documentation. 我不知道它我建议你在文档中阅读它。 http://docs.oracle.com/javase/tutorial/java/generics/index.html
http://docs.oracle.com/javase/tutorial/java/generics/index.html
Generics works with Objects so I think you can't pass a String[] and a int[] in the same method. 泛型与Objects一起使用,所以我认为你不能在同一个方法中传递String []和int []。 Use Integer[] instead.
请改用Integer []。
Here is the code I would use: 这是我将使用的代码:
public static <K> int length(K[] array){
return array.length;
}
Also this could work: 这也可行:
public static int length(Object[] array){
return array.length;
}
But it won't allow you to use a specific type of Object instead of Object class. 但它不允许您使用特定类型的Object而不是Object类。
I'm quite new in this, maybe there is a better solution, but it's the only I know! 我是新手,也许有更好的解决方案,但这是我唯一知道的!
As some others have mentioned, there is now way to do this if you want to be able to accept arrays of primitive types as well (unless you are doing something like in Elliott's solution where you only need and Object and can get by only using methods like Array.getLength(Object) that takes an Object as input). 正如其他人提到的那样,如果你想要能够接受原始类型的数组,现在有办法做到这一点(除非你在Elliott的解决方案中做一些你只需要和Object的东西,并且只能使用方法比如将Object作为输入的Array.getLength(Object)。
What I do is make a generic method and then overload it for each primitive array type like so: 我做的是制作一个泛型方法,然后为每个基本数组类型重载它,如下所示:
public <T> void foo(T[] array) {
//Do something
}
public void foo(double[] array) {
Double[] dblArray = new Double[array.length];
for(int i = 0; i < array.length; i++){
dblArray[i] = array[i];
}
foo(dblArray);
}
//Repeat for int[], float[], long[], byte[], short[], char[], and boolean[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.