Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2005

Tip: Looking for answers? Try searching our database.

Splitting Vector into smalller sub-Vectors

Thread view: 
Allan Bruce - 26 May 2005 10:32 GMT
Is it possible to efficiently split a large Vector into smaller sub-Vectors
without iterating through it and adding the Objects to the sub-Vectors?  If
not, is there a collection which allows this?  I dont need random access, I
am just adding blindly to a single collection, then want to split it up at
the end.

Thanks.
Allan
Boudewijn Dijkstra - 26 May 2005 11:01 GMT
> Is it possible to efficiently split a large Vector into smaller sub-Vectors
> without iterating through it and adding the Objects to the sub-Vectors?  If
> not, is there a collection which allows this?  I dont need random access, I
> am just adding blindly to a single collection, then want to split it up at
> the end.

java.util.List#subList(int,int)
Allan Bruce - 27 May 2005 13:07 GMT
>>Is it possible to efficiently split a large Vector into smaller sub-Vectors
>>without iterating through it and adding the Objects to the sub-Vectors?  If
[quoted text clipped - 3 lines]
>
> java.util.List#subList(int,int)

I tried using ArrayList for this but I get a ClassCastException error at
the following line:

lArray[j] = (ArrayList)lStates.subList(lStart, lEnd);

What can I do to avoid this?  I read the docs but cant see anything that
says what to do.

Thanks.
Allan
Virgil Green - 27 May 2005 23:35 GMT
>>> Is it possible to efficiently split a large Vector into smaller
>>> sub-Vectors without iterating through it and adding the Objects to
[quoted text clipped - 14 lines]
> Thanks.
> Allan

sublist doesn't split the list. It only provides a view of part of the
underlying list. It does not create a new object. You can't cast it the way
you want because the object that is returned is a RandomAccessSubList, not
an ArrayList.

If you want separate ArrayLists to be created, use

lArray[j] = new ArrayList(lStates.subList(lStart, lEnd));

Which takes the List (or RandamAccessSubList) object from sublist and uses
it to create a new ArrayList.

Signature

Virgil



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.