简体   繁体   English

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

[英]How to iterate from last to first an ArrayList?

I wonder if is possible to iterate from last to the first element in an ArrayList, if so how? 我想知道是否可以从ArrayList的最后一个元素迭代到第一个元素,如果可以的话如何? The reason is because I need to remove the last element added to the list 原因是因为我需要删除添加到列表中的最后一个元素

While you can always iterate your ArrayList using 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 : 您最好使用ListIterator<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 docs say: "An initial call to previous would return the element with the specified index minus one." 迭代器的初始索引应等于最后一个元素的索引+1。正如文档所说:“对previous的初始调用将返回具有指定索引减去1的元素。”

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);
}

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. 希望这可以帮助。

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
}
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--;
}

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

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