How to delete elements in ArrayList from the indicated index

As shown in the figure, after running one method at a time, I want to delete the old item and prepare for the next calculation, but I want to know how to delete the elements in ArrayList, such as queue, from the indicated index, and obey the FIFO algorithm?

Solution

You can use list #sublist (int, int):

List<Integer> list =  ...
list = list.subList(10,list.size()); // creates a new list from the old starting from the 10th element

Or, because the sublist creates a view, each modification will affect the original list, which may be better:

List<Integer> list = ...
list.subList(0,10).clear(); // clears the first 10 elements of list
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>