简体   繁体   English

访问嵌套LinkedList中项目的子项目

[英]Accessing sub-item of an item in a nested LinkedList

I want to access an item within an item of nested LinkedList. 我想访问嵌套LinkedList项中的一个项。 The get(index) function gets you the complete item. get(index)函数可为您提供完整的项目。 Is there any method to get item within an item. 是否有任何方法可以获取项目中的项目。 eg I have a nested LinkedList with items as : [ [[1],[2],[3]], [[4],[5],[6]], [[7],[8],[9]] ] . 例如,我有一个嵌套的LinkedList,其中包含以下项: [ [[1],[2],[3]], [[4],[5],[6]], [[7],[8],[9]] ] When I use get(0) , it returns [[1],[2],[3]] . 当我使用get(0) ,它返回[[1],[2],[3]] Now I want to access item [1], [2] and [3] separately but don't know how to do it. 现在,我想分别访问项目[1],[2]和[3],但不知道如何执行。 I know that there will be some method to do it but I am new to java, so need help. 我知道会有一些方法可以做到,但是我是java的新手,所以需要帮助。 Thanks. 谢谢。

假设您使用的是类似LinkedList<LinkedList<Integer>> ,请使用get(0).get(index)

The get() method returns you a element so you either assign it to a object and use it from there or just call the methods inline. get()方法返回一个元素,因此您可以将其分配给一个对象并从那里使用它,或者直接内联调用这些方法。

1 1个

LinkedList foo = myList.get(index);
int bar = foo.get(newIndex);

2 2

int foo = myList.get(index).get(newIndex);

I rather use the first one because it's easier to read. 我更喜欢使用第一个,因为它更易于阅读。

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

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