Do I really need to implement it myself? Answer Create a sublist with the range of elements you wish to remove and then call clear on the returned list. This approach is mentioned as an idiom in the documentation for both List and ArrayList. Here’s a fully unit tested code example!
Tag: arraylist
Initialization of an ArrayList in one line
I wanted to create a list of options for testing purposes. At first, I did this: Then, I refactored the code as follows: Is there a better way to do this? Answer Actually, probably the “best” way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way: The
When to use LinkedList over ArrayList in Java?
I’ve always been one to simply use: I use the interface as the type name for portability, so that when I ask questions such as this, I can rework my code. When should LinkedList be used over ArrayList and vice-versa? Answer Summary ArrayList with ArrayDeque are preferable in many more use-cases than LinkedList. If you’re not sure — just start with