我们应该为object.toArray()使用类型转换吗?

时间:2021-10-20 18:09:23
String[] a = c.toArray(new String[0]);

First: Do I need type cast here? (I think we should write like (String[])c.toArray(); BUT I have seen it as just c.toArray() without using type cast. Is this valid?

第一:我需要在这里打字吗? (我认为我们应该像(String [])c.toArray();但我已经把它看成只是c.toArray()而没有使用类型转换。这有效吗?

Second: Also why we write new String[0]?

第二:为什么我们写新的String [0]?

3 个解决方案

#1


12  

The type cast is usually only needed if you're using pre-generics Java. If you look at the docs for Collection.toArray(T[]) you'll see that it knows that the type of the array which is returned is the same as the type of array passed in. So, you can write:

通常只有在使用pre-generics Java时才需要类型转换。如果你查看Collection.toArray(T [])的文档,你会发现它知道返回的数组的类型与传入的数组的类型相同。所以,你可以这样写:

List<String> list = new ArrayList<String>();
list.add("Foo");
String[] array = list.toArray(new String[0]);

You pass in the array to tell the collection what result type you want, which may not be the same as the type in the collection. For example, if you have a List<OutputStream> you may want to convert that to an Object[], a Stream[] or a ByteArrayOutputStream[] - obviously the latter is only going to work if every element actually is a ByteArrayOutputStream. It also means that if you already have an array of the right type and size, the contents can be copied into that instead of creating a new array.

您传入数组以告诉集合您想要的结果类型,这可能与集合中的类型不同。例如,如果您有一个List ,您可能希望将其转换为Object [],Stream []或ByteArrayOutputStream [] - 显然后者只有在每个元素实际上都是ByteArrayOutputStream时才能工作。这也意味着如果您已经拥有正确类型和大小的数组,则可以将内容复制到该数组中,而不是创建新数组。

A previous version of this answer was inaccurate, by the way - if you use the overload which doesn't take any parameters, you always get back an Object[] which can't be cast:

顺便说一下,这个答案的先前版本是不准确的 - 如果你使用不带任何参数的重载,你总是得到一个无法转换的Object []:

List<String> list = new ArrayList<String>();
list.add("Foo");

// toArray doesn't know the type of array to create
// due to type erasure
Object[] array = list.toArray();

// This cast will fail at execution time
String[] stringArray = (String[]) arrray;

EDIT: I've just noticed this is also mentioned in erdemoo's answer, but it can't hurt to have it here too :)

编辑:我刚刚注意到这也是在erdemoo的答案中提到的,但是在这里也可以不受伤害:)

#2


4  

if you are using list.toArray(), it will return you Object array. Casting to String array will throw exception even if elements stored in list are String type.

如果你使用list.toArray(),它将返回你的Object数组。即使存储在列表中的元素是String类型,强制转换为String数组也会抛出异常。

if you are using list.toArray(Object[] a), it will store elements in list to "a" array. If the elements inside the list are String and you give String array then it will store elements inside String array, if given array is not large enough to store elements inside the list, then it will expand given list.

如果您使用list.toArray(Object [] a),它会将列表中的元素存储到“a”数组中。如果列表中的元素是String并且您给出String数组,那么它将在String数组中存储元素,如果给定的数组不足以在列表中存储元素,那么它将扩展给定列表。

#3


1  

  1. Yes you need the downcast since toArray's returns type is Object[].

    是的,你需要向下转换,因为toArray的返回类型是Object []。

  2. You need to pass (new String[0]) as a parameter since the method needs to know what kind of array it should return (array of strings, Dates, etc.) Internally all list elements are actually objects so the list does not know the type of elements it is holding and therefore it does not know which kind of array it should return, unless you provide it as a parameter.

    你需要传递(new String [0])作为参数,因为该方法需要知道它应该返回什么类型的数组(字符串数组,日期等)。在内部所有列表元素实际上都是对象,所以列表不知道它所持有的元素类型,因此它不知道它应返回哪种数组,除非您将其作为参数提供。

#1


12  

The type cast is usually only needed if you're using pre-generics Java. If you look at the docs for Collection.toArray(T[]) you'll see that it knows that the type of the array which is returned is the same as the type of array passed in. So, you can write:

通常只有在使用pre-generics Java时才需要类型转换。如果你查看Collection.toArray(T [])的文档,你会发现它知道返回的数组的类型与传入的数组的类型相同。所以,你可以这样写:

List<String> list = new ArrayList<String>();
list.add("Foo");
String[] array = list.toArray(new String[0]);

You pass in the array to tell the collection what result type you want, which may not be the same as the type in the collection. For example, if you have a List<OutputStream> you may want to convert that to an Object[], a Stream[] or a ByteArrayOutputStream[] - obviously the latter is only going to work if every element actually is a ByteArrayOutputStream. It also means that if you already have an array of the right type and size, the contents can be copied into that instead of creating a new array.

您传入数组以告诉集合您想要的结果类型,这可能与集合中的类型不同。例如,如果您有一个List ,您可能希望将其转换为Object [],Stream []或ByteArrayOutputStream [] - 显然后者只有在每个元素实际上都是ByteArrayOutputStream时才能工作。这也意味着如果您已经拥有正确类型和大小的数组,则可以将内容复制到该数组中,而不是创建新数组。

A previous version of this answer was inaccurate, by the way - if you use the overload which doesn't take any parameters, you always get back an Object[] which can't be cast:

顺便说一下,这个答案的先前版本是不准确的 - 如果你使用不带任何参数的重载,你总是得到一个无法转换的Object []:

List<String> list = new ArrayList<String>();
list.add("Foo");

// toArray doesn't know the type of array to create
// due to type erasure
Object[] array = list.toArray();

// This cast will fail at execution time
String[] stringArray = (String[]) arrray;

EDIT: I've just noticed this is also mentioned in erdemoo's answer, but it can't hurt to have it here too :)

编辑:我刚刚注意到这也是在erdemoo的答案中提到的,但是在这里也可以不受伤害:)

#2


4  

if you are using list.toArray(), it will return you Object array. Casting to String array will throw exception even if elements stored in list are String type.

如果你使用list.toArray(),它将返回你的Object数组。即使存储在列表中的元素是String类型,强制转换为String数组也会抛出异常。

if you are using list.toArray(Object[] a), it will store elements in list to "a" array. If the elements inside the list are String and you give String array then it will store elements inside String array, if given array is not large enough to store elements inside the list, then it will expand given list.

如果您使用list.toArray(Object [] a),它会将列表中的元素存储到“a”数组中。如果列表中的元素是String并且您给出String数组,那么它将在String数组中存储元素,如果给定的数组不足以在列表中存储元素,那么它将扩展给定列表。

#3


1  

  1. Yes you need the downcast since toArray's returns type is Object[].

    是的,你需要向下转换,因为toArray的返回类型是Object []。

  2. You need to pass (new String[0]) as a parameter since the method needs to know what kind of array it should return (array of strings, Dates, etc.) Internally all list elements are actually objects so the list does not know the type of elements it is holding and therefore it does not know which kind of array it should return, unless you provide it as a parameter.

    你需要传递(new String [0])作为参数,因为该方法需要知道它应该返回什么类型的数组(字符串数组,日期等)。在内部所有列表元素实际上都是对象,所以列表不知道它所持有的元素类型,因此它不知道它应返回哪种数组,除非您将其作为参数提供。