Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a method to remove ArrayList elements from index x to index y

As the title says, if I'm creating a method to remove a section of an ArrayList, how would I go about this.

If I have ArrayList<Character> se = {'a','b','c','d','e','f','g'} and want to return only "abg" I would call this method as such:

remove(2,5);

How could I create this method to remove not only the arg indexes but also everything in between?

like image 466
Alkarin Avatar asked Feb 07 '23 11:02

Alkarin


2 Answers

list.subList(2, 6).clear()

does the job in one step.

like image 50
Louis Wasserman Avatar answered Feb 15 '23 10:02

Louis Wasserman


Create a loop counting backward from 5 to 2 and remove the elements.

for(int i = 5; i >= 2; i--) ...
like image 43
Sason Ohanian Avatar answered Feb 15 '23 09:02

Sason Ohanian