How do I get an array slice of an ArrayList
in Java? Specifically I want to do something like this:
ArrayList inputA = input.sub
If there is no existing method then I guess you can iterate from 0 to input.size()/2
, taking each consecutive element and appending it to a new ArrayList.
EDIT: Actually, I think you can take that List and use it to instantiate a new ArrayList using one of the ArrayList constructors.
I have found a way if you know startIndex and endIndex of the elements one need to remove from ArrayList
Let al
be the original ArrayList and startIndex
,endIndex
be start and end index to be removed from the array respectively:
al.subList(startIndex, endIndex + 1).clear();
Although this post is very old. In case if somebody is looking for this..
Guava facilitates partitioning the List into sublists of a specified size
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> subSets = Lists.partition(intList, 3);
This is how I solved it. I forgot that sublist was a direct reference to the elements in the original list, so it makes sense why it wouldn't work.
ArrayList<Integer> inputA = new ArrayList<Integer>(input.subList(0, input.size()/2));
In Java, it is good practice to use interface types rather than concrete classes in APIs.
Your problem is that you are using ArrayList
(probably in lots of places) where you should really be using List
. As a result you created problems for yourself with an unnecessary constraint that the list is an ArrayList
.
This is what your code should look like:
List input = new ArrayList(...);
public void doSomething(List input) {
List inputA = input.subList(0, input.size()/2);
...
}
this.doSomething(input);
Your proposed "solution" to the problem was/is this:
new ArrayList(input.subList(0, input.size()/2))
That works by making a copy of the sublist. It is not a slice in the normal sense. Furthermore, if the sublist is big, then making the copy will be expensive.
If you are constrained by APIs that you cannot change, such that you have to declare inputA
as an ArrayList
, you might be able to implement a custom subclass of ArrayList
in which the subList
method returns a subclass of ArrayList
. However:
ArrayList
class.ArrayList
instances to create instances of your subclass instead.The "copy the array" solution is more practical ... bearing in mind that these are not true slices.