【问题标题】:How can I access and iterate through an ArrayList of Linked list element?如何访问和遍历链表元素的 ArrayList?
【发布时间】:2021-03-22 17:28:24
【问题描述】:

我正在尝试编写图 ADT 的邻接表实现代码。

 private ArrayList<LinkedList<Integer>> adjLists ;

但我不知道如何遍历图形的顶点...

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 类似于 List>。您可以通过获取第一个列表的每个项目,然后再次获取第二个列表的每个项目来做到这一点。例如 get(0).get(0).getObject()
  • @ΣωτήρηςΡαφαήλ 这个不适合我!!我也想遍历整个列表的所有元素。

标签: java list arraylist graph linked-list


【解决方案1】:

errr...您只需遍历每个元素...

for(int i = 0;i<adjLists.size();i++){
   for(int j = 0;j<adjLists.size();j++){
       int element = adjLists.get(i).get(j);
        //Then do whatever you want
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-18
    • 2012-11-19
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多