Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-27-2008, 11:22 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Limiting size of ArrayList
Hi guys,

ArrayList can grow as the elements are added to it. Is there a was to define the upper limit of the ArrayList? For example, I want my ArrayList to throw some Exception when 101th element is added.

Thanks for your time.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 12:07 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
You're probably looking for an API defined way to accomplish this, and I'm not certain there is one. Therefore, there shouldn't be anything wrong with using a standard control statement, ie: for loop. Soo...
Code:
1) for all elements up to and including 100 // ..... do something... // if attempt to add past 100 // throw exception(which is kinda of pointless since that's what the control // statement was for - only elements up to an including 100)
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 08:22 AM
Member
 
Join Date: Jan 2008
Posts: 20
JAdmin is on a distinguished road
Agree with Captain. You cannot limit the size of an ArrayList. You will have to come up with a mechanism to achieve this.

You may subclass the ArrayList and implement your own code to prevent the size from growing beyond a limit.

Something like

public final class MyList extends ArrayList{

...
....
public boolean add(Object o) throws Throwable{
if(this.size() < 100)
//add
else
throw new MaxSizeReachedException("Limit reached");

}

}

note : The above code is a kind of sudo code, just to give you an idea of what we meant.

Hope this helps!
__________________
Sincerely, Your friends at
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-29-2008, 08:37 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Yeah i would personally just do something like
Code:
ArrayList<String> list = new ArrayList<String(); if(list.get(100) != null) //If there exists a 101th element { throw new ArrayIndexOutOfBoundsException(); }
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ResultSet size bugger Database 15 09-18-2008 04:52 PM
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 01:43 PM
Limiting the capacity of a cell of JTable rameshraj Advanced Java 0 03-24-2008 04:20 PM
File size eva New To Java 2 12-19-2007 11:27 AM
Object size kavithas New To Java 0 11-30-2007 02:00 PM


All times are GMT +3. The time now is 01:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org