将单个对象转换为数组的函数?

时间:2022-10-22 23:21:03

I wanted to write a function that would take an object and convert it to an array that contains that object as a single element. It occurred to me that I could maybe do this with generics and variable arguments to essentially do this automatically, without the need to write a function for each object type I wished to use. Will this code work? Are there any subtleties I need to be aware of?

我想编写一个函数,它将获取一个对象并将其转换为包含该对象作为单个元素的数组。在我看来,我可以使用泛型和变量参数来自动执行此操作,而无需为我希望使用的每个对象类型编写函数。这段代码会起作用吗?我需要注意哪些细微之处?

public static <X> X[] convert_to_array(X... in_objs){
    return in_objs;
}

3 个解决方案

#1


It works but it seems like:

它工作但看起来像:

 Object o = new Object();
 someMethod(new Object[] { o } );

is a little more straightforward then:

然后更简单一点:

Object o = new Object();
someMethod(convert_to_array(o));

In cases where sometimes I want to pass a single object, but other times I want to pass an array, usually I just use an overloaded method in the API:

在有时我想传递单个对象的情况下,但有时我想传递一个数组,通常我只是在API中使用重载方法:

public void doSomething(Object o)
{
    doSomething(new Object[] { o } );
}

public void doSomething(Object[] array)
{
    // stuff goes here.
}

Varargs can be used but only if the array is the last parameter of course.

可以使用Varargs,但前提是阵列是最后一个参数。

#2


Why not simply:

为什么不简单:

Object o = new Object();
Object[] array = { o }; // no method call required!

What are you really trying to accomplish?

你真的想要完成什么?

#3


Assuming you need a that you need an array that is properly typed, you can use java.lang.reflect.Array:

假设你需要一个你需要一个正确类型的数组,你可以使用java.lang.reflect.Array:

static public Object[] createTypedArray(Object elm) {
    Object[] arr=(Object[])java.lang.reflect.Array.newInstance(elm.getClass(),1);
    arr[0]=elm;
    return arr; // this can be cast safely to an array of the type of elm
    }

#1


It works but it seems like:

它工作但看起来像:

 Object o = new Object();
 someMethod(new Object[] { o } );

is a little more straightforward then:

然后更简单一点:

Object o = new Object();
someMethod(convert_to_array(o));

In cases where sometimes I want to pass a single object, but other times I want to pass an array, usually I just use an overloaded method in the API:

在有时我想传递单个对象的情况下,但有时我想传递一个数组,通常我只是在API中使用重载方法:

public void doSomething(Object o)
{
    doSomething(new Object[] { o } );
}

public void doSomething(Object[] array)
{
    // stuff goes here.
}

Varargs can be used but only if the array is the last parameter of course.

可以使用Varargs,但前提是阵列是最后一个参数。

#2


Why not simply:

为什么不简单:

Object o = new Object();
Object[] array = { o }; // no method call required!

What are you really trying to accomplish?

你真的想要完成什么?

#3


Assuming you need a that you need an array that is properly typed, you can use java.lang.reflect.Array:

假设你需要一个你需要一个正确类型的数组,你可以使用java.lang.reflect.Array:

static public Object[] createTypedArray(Object elm) {
    Object[] arr=(Object[])java.lang.reflect.Array.newInstance(elm.getClass(),1);
    arr[0]=elm;
    return arr; // this can be cast safely to an array of the type of elm
    }