gpt4 book ai didi

java - 如何制作从列表末尾开始的列表迭代器

转载 作者:行者123 更新时间:2023-12-01 22:36:30 25 4
gpt4 key购买 nike

我有一个家庭作业问题要求我制作一个从列表末尾开始的列表迭代器。我不知道该怎么做。这是代码

public class LinkedListTester7
{
public static void main(String [] args)
{
LinkedList<Integer> list = new LinkedList<Integer>();
for (int i = 0 ; i < 100 ; i = i + 2)
list.add(i) ;
//-----------Start below here. To do: approximate lines of code = 3
// 1. make a list iterator that starts at the end of the list ;
ListIterator<Integer> litr = list.listIterator();
//2. while hasPrevious ;
while (litr.hasPrevious()) {
//3. print what is returned by previous() followed by a blank without a newline
System.out.println(litr.previous()+ " ");
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
System.out.println() ;
}
}

最佳答案

阅读 java.util.List API 文档...

ListIterator<Integer> litr = list.listIterator(list.size());

关于java - 如何制作从列表末尾开始的列表迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22262832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com