Java 如何从最后一个迭代到第一个ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19233466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:17:44  来源:igfitidea点击:

How to iterate from last to first an ArrayList?

javaiteratoriteration

提问by Ricardo Sanchez

I wonder if is possible to iterate from last to the first element in an ArrayList, if so how? The reason is because I need to remove the last element added to the list

我想知道是否可以从 ArrayList 中的最后一个元素迭代到第一个元素,如果可以,如何?原因是因为我需要删除添加到列表中的最后一个元素

采纳答案by dasblinkenlight

While you can always iterate your ArrayListusing an index, like this

虽然您始终可以ArrayList像这样使用索引进行迭代

List<String> myList = new ArrayList<String>();
... add code to populate the list with some data...
for (int i = myList.size() - 1 ; i >= 0 ; i--) {
    if (myList.get(i).equals("delete me")) {
        myList.remove(i);
    }
}

you would be better off using a ListIterator<T>for the purpose of deleting items from your ArrayList:

您最好使用 aListIterator<T>来从您的 中删除项目ArrayList

ListIterator<String> listIter = myList.listIterator();
while (listIter.hasNext()) {
    if (listIter.next().equals("delete me")) {
        listIter.remove();
    }
}

List iterators can be used to iterate your list backwards, too -- like this:

列表迭代器也可用于向后迭代您的列表——就像这样:

ListIterator<String> listIter = myList.listIterator(myList.size());
while (listIter.hasPrevious()) {
    String prev = listIter.previous();
    // Do something with prev here
}

The initial index of the iterator should be equal to the index of the last element + 1. As the docssay: "An initial call to previouswould return the element with the specified index minus one."

迭代器的初始索引应该等于最后一个元素的索引 + 1。正如文档所说:“初始调用previous将返回具有指定索引减一的元素。”

回答by Dom

If you need to just remove the last element of an array list, you could just do:

如果您只需要删除数组列表的最后一个元素,您可以这样做:

arrayList.remove(arrayList.size()-1);

If you wanted to remove all elements from back to front, you could use this for loop:

如果你想从后到前删除所有元素,你可以使用这个 for 循环:

for(int i = arraList.size()-1; i >= 0; i--)
{
       arrayList.remove(i);
}

For more infomation on Array Lists, you can go here: http://www.tutorialspoint.com/java/java_arraylist_class.htm

有关数组列表的更多信息,您可以访问这里:http: //www.tutorialspoint.com/java/java_arraylist_class.htm

Hope this helps.

希望这可以帮助。

回答by Michael Easter

Here is one way to do it (your reason why is a separate question):

这是一种方法(您的原因是一个单独的问题):

import java.util.*;

List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");

ListIterator<String> iter = list.listIterator(list.size());

while (iter.hasPrevious()) {
    String s = iter.previous();
    System.out.println(s);
}

回答by melc

ArrayList aList = new ArrayList();
aList.add(1);
aList.add(2);
aList.add(3);

Collections.reverse(aList);

Iterator iter = aList.iterator();
while(iter.hasNext()){
    iter.next();
    iter.remove();//remove an element
    break;//break from the loop or something else
}

回答by jdev

List<String> names = ....
int count= names.getSize();
Iterator<String> i = names.iterator();
while (i.hasNext()) {
   String s = i.next(); // must be called before you can call i.remove()
   // Do something
   if (count==1)
      i.remove();
   count--;
}