无法从Java中的深层ArrayList获取值

发布于 2024-12-10 20:43:13 字数 815 浏览 0 评论 0 原文

我尝试在我拥有的 ArrayList 中存储和获取数字。我在我的班级中有这个:

private ArrayList<ArrayList> possibleNumbers = new ArrayList<ArrayList>();

然后我用空数组列表填充它,以确保每个 y 都有一些东西

for( int x = 0; x < grid[ y ].length; x++ )
{
    possibleNumbers.get( y ).add( x , new ArrayList<Integer>());
}

现在在我的代码中,我有一个 ArrayList,变量“列表”中设置了几个数字该列表可能的数字:

possibleNumbers.get( y ).set( x, list );

稍后我尝试从插入的列表中获取值:

possibleNumbers.get( y ).get( x ).get( 0 );

但现在我收到错误:

symbol  : method get(int)
location: class java.lang.Object
possibleNumbers.get( y ).get( x ).get(0);
                                 ^

我真的找不到为什么第三个获取不起作用但第二个获取不起作用...我希望有人可以帮我。

谢谢,

克里斯

I am try to store and get numbers in an ArrayList i have. I have this in my class:

private ArrayList<ArrayList> possibleNumbers = new ArrayList<ArrayList>();

Then i fill it with empty array lists to make sure there is something in it for each y

for( int x = 0; x < grid[ y ].length; x++ )
{
    possibleNumbers.get( y ).add( x , new ArrayList<Integer>());
}

Now far later in my code i have an ArrayList with a couple of numbers in the variable 'list', is set that list to possible numbers:

possibleNumbers.get( y ).set( x, list );

Later i try to get the value from the inserted list:

possibleNumbers.get( y ).get( x ).get( 0 );

But now i get an error:

symbol  : method get(int)
location: class java.lang.Object
possibleNumbers.get( y ).get( x ).get(0);
                                 ^

I really can't find why this third get doesn't work but the second does... I hope someone can help me.

Thanks,

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒眉千度 2024-12-17 20:43:13

我假设 possibleNumbers 的类型为 ArrayList>。因此,从 possibleNumbers.get( y ).get( x ) 返回的对象被视为 Object。如果你施放它会发生什么?

I assume that possibleNumbers is of type ArrayList<ArrayList<Object>>. And so, the object returned from possibleNumbers.get( y ).get( x ) is considered to be an Object. What occurs if you cast it?

明明#如月 2024-12-17 20:43:13

问题是您试图深入三层结构:

possibleNumbers.get( y ).get( x ).get( 0 )
  • possibleNumbers 是一个 ArrayList>
  • possibleNumbers.get (y) 是一个 ArrayList
  • possibleNumbers.get(y).get(x) 是一个Object

您无法对Object 调用get(0)

The problem is that you are trying to go three levels deeps into a two-level structure:

possibleNumbers.get( y ).get( x ).get( 0 )
  • possibleNumbers is an ArrayList<ArrayList<Object>>
  • possibleNumbers.get(y) is an ArrayList<Object>
  • possibleNumbers.get(y).get(x) is an Object

You can't call get(0) on an Object.

£噩梦荏苒 2024-12-17 20:43:13

您有一个包含列表的列表:

possibleNumbers //ArrayList<ArrayList>

第一个 get 将返回这些列表之一:

posibleNumbers.get(y) //-> ArrayList

第二个 get 将获取该列表的一个条目:

possibleNumbers.get(y).get(x) //-> list entry

因此最终的 get(0) 也是一个许多。

You have a list that contains lists:

possibleNumbers //ArrayList<ArrayList>

The first get is going to return one of those lists:

posibleNumbers.get(y) //-> ArrayList

The second get is going to get one entry of that list:

possibleNumbers.get(y).get(x) //-> list entry

So that final get(0) is one too many.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文