Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I slice an ArrayList out of an ArrayList in Java?

Tags:

java

arraylist

How do I get an array slice of an ArrayList in Java? Specifically I want to do something like this:

ArrayList<Integer> inputA = input.subList(0, input.size()/2); // where 'input' is a prepouplated ArrayList<Integer> 

So I expected this to work, but Java returns a List - so it's incompatible. And when I try to cast it, Java won't let me. I need an ArrayList - what can I do?

like image 645
B T Avatar asked Sep 26 '09 07:09

B T


People also ask

Can we slice ArrayList in Java?

As a result you created problems for yourself with an unnecessary constraint that the list is an ArrayList . 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.

What is the way to get subList from an ArrayList?

ArrayList. subList() method. This method takes two parameters i.e. the start index for the sub-list(inclusive) and the end index for the sub-list(exclusive) from the required ArrayList. If the start index and the end index are the same, then an empty sub-list is returned.


2 Answers

In Java, it is good practice to use interface types rather than concrete classes in APIs.

Your problem is that you1 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); 

1 - Based on your comments, "you" was actually someone else ... who set this problem in an interview question. It is possible that this was actually a trick question, designed to see how you would cope with creating a (real) slice of an ArrayList that was a assignment compatible with ArrayList.


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:

  1. It would be a lot of work to design, implement and test.
  2. You have now added significant new class to your code base, possibly with dependencies on undocumented aspects (and therefore "subject to change") aspects of the ArrayList class.
  3. You would need to change relevant places in your codebase where you are creating 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.

like image 76
Stephen C Avatar answered Oct 09 '22 11:10

Stephen C


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(); 
like image 45
Aman Gupta Avatar answered Oct 09 '22 12:10

Aman Gupta