aspose file tools*
The moose likes Beginning Java and the fly likes sublist of arraylist??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sublist of arraylist???" Watch "sublist of arraylist???" New topic
Author

sublist of arraylist???

Suzi Kapoor
Greenhorn

Joined: May 04, 2006
Posts: 4
Hi,

I was wondering if there is an efficient way of creating a sublist from an arraylist( not List)
Suzi Kapoor
Greenhorn

Joined: May 04, 2006
Posts: 4
I searched and realised that Generics could be used.
Could someone explain casting a arraylsit into list with some example.

Thanks in advance
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
An ArrayList is a List (it implements the List interface).

[ May 04, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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).

Demo:


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:


There is no emoticon for what I am feeling!
Suzi Kapoor
Greenhorn

Joined: May 04, 2006
Posts: 4
Thanks for ur help.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sublist of arraylist???