简体   繁体   English

如何在 java 中将 Object[] 转换为 int[]

[英]How to cast Object[] into int[] in java

After I use the method .toArray() in a Priority Queue of Integers I get an array of Objects like this:在整数优先级队列中使用.toArray()方法后,我得到一个对象数组,如下所示:

Object[] objects = pq.toArray();

However, I need the array to be int[] .但是,我需要数组是int[] I've tried:我试过了:

int [] arr = (int[]) objects;

but it says:但它说:

Cannot cast from Object[] to int[]

How can I achieve this?我怎样才能做到这一点?

All collections provide an alternative toArray implementation specifying the element type:所有toArray都提供了指定元素类型的数组实现的替代方案:

Integer[] objects = pq.toArray(Integer[]::new);

Note this requires Java v11 or higher请注意,这需要 Java v11 或更高版本

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#toArray(java.util.function.IntFunction) https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#toArray(java.util.ZC1C425268IntC1794)

Note the resultant array is obviously integer wrapper objects in this case, don't know if that's fine for your requirements.请注意,在这种情况下,结果数组显然是 integer包装器对象,不知道这是否符合您的要求。

EDIT - quick test:编辑 - 快速测试:

PriorityQueue<Integer> q = new PriorityQueue<>();
q.add(42);
System.out.println(Arrays.toString(q.toArray(Integer[]::new)));

This is not something you can easily do.这不是你可以轻易做到的。

You might know that the Object[] is actually just full of integers, which would mean that it should be possible to cast it, right?您可能知道 Object[] 实际上只是充满了整数,这意味着应该可以强制转换它,对吧?

Problem is, the variable "objects" still exists.问题是,变量“对象”仍然存在。 And in an Object[] you're allowed to put any Object.在 Object[] 中,您可以放置任何 Object。 So after your cast, someone might do something like:所以在你的演员之后,有人可能会做类似的事情:

objects[0] = "some string";

So what should happen then?那么应该怎么办呢? Should your int[] suddenly be invalid since it now contains a String?你的 int[] 是否应该突然无效,因为它现在包含一个字符串? Should it not be allowed to input things other than Integers into objects which typically allows that?是否应该不允许将除整数以外的东西输入通常允许这样做的objects中?

Casting just means you point to the same thing but look at it differently.铸造只是意味着您指向同一事物,但以不同的方式看待它。 What you need to do is to transform it.你需要做的就是改造它。 For that there's already an answer:为此,已经有了答案:

java-map-an-array-of-strings-to-an-array-of-integers java-map-an-array-of-strings-to-an-array-of-integers

So, simple casting is not going to help out here.所以,简单的铸造在这里没有帮助。

Update: @stridecolossus answer is probably just as good as the one I linked to.更新:@stridecolossus 的答案可能和我链接的答案一样好。

Since PriorityQueue implements Collection , you can use a stream to iterate over its elements and map them to int :由于PriorityQueue实现Collection ,您可以使用stream迭代其元素和 map 它们到int

PriorityQueue<Integer> pq = new PriorityQueue<>(Arrays.asList(1, 2, 5));

int[] arr = pq.stream().mapToInt(Integer::intValue).toArray();

System.out.println(Arrays.toString(arr)); // [1, 2, 5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM