java - Converting a subList of an ArrayList to an ArrayList -


im using arraylist , im trying copy part of arraylist therefore im using:

sibling.keys = (arraylist<integer>) keys.sublist(mid, this.num); 

where "sibling.keys" new arraylist , "keys or this.keys" older arraylist. used casting because eclipse told me throws classcastexception:

java.util.arraylist$sublist cannot cast java.util.arraylist

any advice?

sublist returns view on existing list. it's not arraylist. can create copy of it:

sibling.keys = new arraylist<integer>(keys.sublist(mid, this.num)); 

or if you're happy view behaviour, try change type of sibling.keys list<integer> instead of arraylist<integer>, don't need make copy:

sibling.keys = keys.sublist(mid, this.num); 

it's important understand difference though - going mutate sibling.keys (e.g. adding values or changing existing elements)? going mutate keys? want mutation of 1 list affect other?


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -