This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Originally posted by Suzi Kapoor: I was wondering if there is an efficient way of creating a sublist from an arraylist( not List)
java.util.ArrayList implements interface java.util.List, so your "not List" restriction doesn't make sense. As far as creating a sublist, check out List method subList. Its API states: Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
The most important thing here is the fact that the sublist is *backed* by the original list, so alterations to the sublist affect the backing list. If you don't want this, copy the sublist (copying deeply if you need to).
Originally posted by Suzi Kapoor: I searched and realised that Generics could be used. Could someone explain casting a arraylsit into list with some example.
Generics are orthogonal to your questions. Here you are asking something much more basic: how to "upcast" an arraylist reference to List. This is implicit in the syntax, for example: