简体   繁体   English

如何使方法将任何类型的数组作为参数?

[英]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 []数组?

Use generics . 使用泛型

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.

相关问题 如何制作一个可以接受任何类型参数的 Java 类 - How to make a Java class that can take parameters of any type 如何减少多个采用不同类型1个参数的方法? - How can I reduce multiple methods that take 1 parameter of different type? 如何为参数列表和/或返回类型中的“任何映射条目”值创建通用方法签名? - How can I create a generic method signature for “any map entry” value in the parameter list and/or return type? 如何使具有有界类型参数的方法排除一个子类? - How can I make a method with a bounded type parameter exclude one subclass? 如何使方法采用空值? - How can I make a method take a null value? 如何使方法参数类型为ArrayList <Object> 采取不同的对象类型 - how to make method parameter type ArrayList<Object> take different object types 如何使此方法返回数组? - How can I make this method return an array? 如何创建带有任何类型参数的通用方法? - How to create generic methods that take any type parameter? 如何调用采用 ServerHttpResponse 类型参数的方法? - How can I call a method which takes a parameter of type ServerHttpResponse? 如何测试Method是否接受参数类型? - How can I test if a Method will accept a parameter type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM