该类型的前pression必须是一个数组类型,但它解析为对象 [英] The type of an expression must be an array type, but it is resolved to Object

查看:218
本文介绍了该类型的前pression必须是一个数组类型,但它解析为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我预计编译,但我一直得到错误之类的前pression必须是一个数组类型,但它解析为对象。有一个简单的解决方法呢?

I expected this to compile, but I kept getting the error "The type of an expression must be an array type, but it is resolved to Object". Is there a simple workaround for this?

public class NodeTest {
    public static void main(String[] args) {

    Object[] arr = new Object[5]; // each element of object will be an array of integers.
    for(int i = 0; i < 5; i++){
        int[][] a = new int[2*(i+1)][2*(i+1)];
        arr[i] = a;
    }
    arr[0][0][0] = 0; //error here
}

}

推荐答案

改编对象[] 那么改编[0] 将返回对象

不过既然你知道改编包含 INT [] [] 实例对象,你将不得不投下他们是这样的。

But since you know that arr contains int[][] as instance of Object you will have to cast them to be so.

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

这篇关于该类型的前pression必须是一个数组类型,但它解析为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆