I'm trying to get a segment from an arrayList
by index and by keeping a reference to the original arrayList, but when I use subList
it returns me a list
. I want to keep my arrayList
structure. For that, a made a cast ( Error: java.util.ArrayList$SubList cannot be cast to java.util.ArrayList)
Source code:
ArrayList< Integer > TAB = new ArrayList< Integer >();
ArrayList< Integer > TAB_Segment = new ArrayList< Integer >();
TAB.add(new Integer( 0 ));
TAB.add(new Integer( 1 ));
TAB.add(new Integer( 2 ));
TAB.add(new Integer( 4 ));
TAB.add(new Integer( 5 ));
TAB_Segment = TAB.subList(0,2); // error
TAB_Segment = (ArrayList<Integer>) TAB.subList(0,2); // error java.util.ArrayList$SubList cannot be cast to java.util.ArrayList)
To preserve my arrayList structure, I find removeRange
. Sadly this function doesn't return anything and it makes the inverse of what I really need. So, is there a way to attend my goal?