我正在尝试使用迭代器打印出大小为 8 的链接列表的数组列表,这是我到目前为止所拥有的
ArrayList<LinkedList> myaol = new ArrayList<>();//my array which the linked lists are created when needed
public void printList(Iterator<Gen> itr)//gen is the generic data type
{
while( this.hasNext() )
{
System.out.println(this.next());
}
}
我了解如何迭代一个链表,但不确定如何转到数组列表的下一个索引以获得每个链表,任何帮助将不胜感激
最佳答案
既然你有一个 LinkedList 的 ArrayList,你可以使用以下代码获取每个 LinkedList,并用 LinkedList 做任何你想做的事情...
ArrayList<LinkedList> myaol = new ArrayList<>();
Iterator<LinkedList> itr = myaol.iterator();
while (itr.hasNext()) {
LinkedList myList = itr.next();
}
您可以像这样打印列表
// print the list
System.out.println("LinkedList:" + myList);
// create an array and copy the list to it
Object[] array = myList.toArray();
// print the array
for (int i = 0; i < myList.size(); i++) {
System.out.println("Array:" + array[i]);
}
关于java - 需要在 java 中使用迭代器的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28974416/