tags:

views:

1175

answers:

4

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?

+2  A: 

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.

Jorge Israel Peña
Thats exactly what I did (posted my answer before I read your edit). Thanks : )
B T
But that *copies* the List to make a new ArrayList.
Joren
Wells its what I was looking for actually, sooooo.... yeah
B T
Well I suppose that's okay then. ;)
Joren
A: 

So I feel like a douche for answering my question 5 minutes after I asked it.. But 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 inputA = new ArrayList(input.subList(0, input.size()/2));

Verbose as all hell. I don't take back what I said about Java.

B T
Wow people really hate that I hate Java. God damn Zealots - just downvote this some more. I expect at least -50!!
B T
With that attitude I will. Apparently our dictionaries define the word "verbose" quite differently, maybe you would like to write an example of how you'd really like to do it?
Esko
B T
+6  A: 

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 by introducing an unnecessary requirement that everything is an ArrayList. No wonder you are frustrated!

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 "solution" to the problem works by making a copy of the list. It is not a slice in the normal sense ... and if the list is big, making the copy could be expensive.

Stephen C
A: 

Java is an innocuous, creeping world-plot that knows you mind like the back roads of Podunkville .... ( et cetera )

I do not expect a cast to work, except as noted by the master who advises casting to the interface "type". That is the canonical approach given. Collections often have an .elementAt() method that will work in a for loop or do / while - I have discovered as you have that one has to "new" ...

Speaking of hate, I discovered last night after drooling through the matter that there is some sort of "don't do it in main()" whereby you cannot get an ArrayList reference even thought you try all sorts of Arrays.asList( new Object[]{ ..., ..., ... } and casting on a return type that is already an array list

As you have observed, Java is a creeping sanity plot run by invisible headless monsters.

Nicholas Jordan