-5

Anyone know what this code does?

I am very confused, and also it doesn't work for some reason even though its in my textbook.

Here is my array list:

   ArrayList<Integer> alist = new ArrayList<Integer>();


  while(!alist.isEmpty()){
      alist.removeLast();
  }

The alist.removeLast(); piece of code has an error in it

Im not sure what this piece of code is supposed to do and how to get it to work.

4

2 回答 2

1

如果这个确切的代码在你的教科书中,那么你的教科书是错误的。 ArrayList.removeLast()不存在。然而,LinkedList.removeLast()确实存在。

看起来这种方法采用了一种非常奇怪的方法来清空列表。

于 2013-05-09T15:22:42.817 回答
1

removeLast()是一种方法LinkedList。要删除 的最后一个元素ArrayList,请使用:

arrayList.remove(arrayList.size() - 1);
于 2013-05-09T15:24:25.343 回答