Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将对象[]强制转换为int[]java?_Java_Types_Casting - Fatal编程技术网

如何将对象[]强制转换为int[]java?

如何将对象[]强制转换为int[]java?,java,types,casting,Java,Types,Casting,这导致了complie错误,我认为这是因为int不是从对象继承的,但有没有解决方案不遍历每个项来执行强制转换 Object[] A1 ={1,2,3,4}; int [] B1 = (int[]) A1; 转换为整数没有问题 Object[] A1 ={1,2,3,4}; Integer [] B1 = (Integer[]) A1; 我知道我们能做到 int [] C = new int[B1.length]; for(int i=0;i<B1.length;i++)

这导致了complie错误,我认为这是因为int不是从对象继承的,但有没有解决方案不遍历每个项来执行强制转换

Object[] A1 ={1,2,3,4};
int [] B1 = (int[]) A1;
转换为整数没有问题

Object[] A1 ={1,2,3,4};
Integer [] B1 = (Integer[]) A1;
我知道我们能做到

   int [] C = new int[B1.length];
   for(int i=0;i<B1.length;i++)
     C[i] = Integer.valueOf(B[i]);
int[]C=新的int[B1.长度];

对于(int i=0;i,正如您在问题中所承认的,
int
不会扩展
Object
,因此强制转换没有意义,编译器会正确地进行投诉

实现这一点最安全、最简单的方法可能是:

Object[] objects = {1, 2, 3, 4};
int[] ints = Arrays.stream(objects).mapToInt(o -> (int)o).toArray();
不是特别优雅,但是同样,在
对象数组中存储整数数组也不是特别优雅。

无法将
对象[]
强制转换为
int[]
,即使源数组中的所有对象都是
整数
对象(如您的示例),因为所有值都需要取消固定,不仅仅是演员

Object[] A1 = {1,2,3,4};
该语句实际上为您自动装箱4个整数文本,因此编译器将其转换为:

Object[] A1 = new Object[4];
A1[0] = Integer.valueOf(1);
A1[1] = Integer.valueOf(2);
A1[2] = Integer.valueOf(3);
A1[3] = Integer.valueOf(4);
从反编译中可以看出:

publicstaticvoidmain(java.lang.String[])抛出java.lang.Exception;
代码:
0:iconst_4
1:anewarray#3//类java/lang/Object
4:dup
5:iconst_0
6:iconst_1
7:invokestatic#19//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
10:aastore
11:dup
12:iconst_1
13:iconst_2
14:invokestatic#19//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
17:aastore
18:dup
19:iconst_2
20:iconst_3
21:invokestatic#19//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
24:aastore
25:dup
26:iconst_3
27:iconst_4
28:invokestatic#19//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
31:aastore
32:astore_1
33:返回
任何解决方案都必须遍历每个项以取消绑定值,无论是显式还是隐式

实现这一点的各种方法:

// Using cast to specific type and auto-unboxing (object cast)
int[] arr1 = new int[A1.length];
for (int i = 0; i < A1.length; i++)
    arr1[i] = (Integer)A1[i];

// Using cast to specific type and explicit unboxing
int[] arr2 = new int[A1.length];
for (int i = 0; i < A1.length; i++)
    arr2[i] = ((Integer)A1[i]).intValue();

// Using cast to more generic type and value extraction
int[] arr3 = new int[A1.length];
for (int i = 0; i < A1.length; i++)
    arr3[i] = ((Number)A1[i]).intValue();

// Using Java 8 streams with method references
int[] arr4 = Arrays.stream(A1)
                   .map(Integer.class::cast)
                   .mapToInt(Integer::intValue)
                   .toArray();

// Using Java 8 streams with lambda expression with auto-unboxing (primitive cast)
int[] arr5 = Arrays.stream(A1)
                   .mapToInt(o -> (int)o)
                   .toArray();
//对特定类型使用强制转换并自动取消装箱(对象强制转换)
int[]arr1=新的int[A1.长度];
对于(int i=0;i(int)o)
.toArray();

否,因为
int
Integer
不同。您可以。您正在尝试的是相反的。既然您已经修复了标题,那么答案是您将数组声明为
Object[]
,所以它永远都是这样。没有向上转换可以改变这一点。
Integer[]B1=(Integer[])A1-这可能不是编译时错误,但是。为什么不
Arrays.stream(objects).mapToInt(o->(int)o)
?如果您担心这一点,
o->(Integer)o
同样有效。