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 08-06-2007, 03:42 AM
Member
 
Join Date: Aug 2007
Location: new yrok
Posts: 3
eNine is on a distinguished road
Help ArrayList.add()
Can someone please tellme what is wrong with my code? I'm new Java but I haven't had any trouble with ArrayList until I tried to write the code below. For some reason, the Rectangle2D 's are not being added to my arrayList.


import java.util.ArrayList;
import java.awt.geom.Rectangle2D;


public class Hat {
ArrayList<Rectangle2D.Double> rectangles = new ArrayList<Rectangle2D.Double>(5);

public Hat(){
}

Hat(double x, double y, double w, double h){
rectangles.add((new Rectangle2D.Double(x,y,w,h) ) );
}

public void countHats(){
System.out.println("There are "+ rectangles.size() +" in list");

}

public static void main(String[] args){
Hat h = new Hat();
int limit =5;
int randomLim = 100;
for(int i=0;i<limit;i++)
{
new Hat(Math.random()*randomLim,Math.random()*randomLi m,Math.random()*randomLim,Math.random()*randomLim) ;
}
h.countHats();
}

}

Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 06:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
You made a new Hat each time with only one rectangle in the ArrayList of the Hat for each trip through the for loop. So you ended up with 5 Hats. Instead, make an add method so you can add rectangles to any Hat. Then you can have a Hat with multiple rectangles. In your h.countHats() statement you get zero because you never did anything with the Hat h you created before the for loop. Try this:
Code:
import java.util.ArrayList; import java.awt.geom.Rectangle2D; public class HatRx { ArrayList<Rectangle2D.Double> rectangles = new ArrayList<Rectangle2D.Double>(5); public HatRx(){} void add(double x, double y, double w, double h){ rectangles.add((new Rectangle2D.Double(x,y,w,h) ) ); } public void countHats(){ System.out.println("There are "+ rectangles.size() +" in list"); } public static void main(String[] args){ HatRx h = new HatRx(); int limit =5; int randomLim = 100; for(int i = 0; i < limit; i++) { h.add( Math.random()*randomLim, Math.random()*randomLim, Math.random()*randomLim, Math.random()*randomLim); } h.countHats(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-06-2007, 03:13 PM
Member
 
Join Date: Aug 2007
Location: new yrok
Posts: 3
eNine is on a distinguished road
thanks. that was exactly my problem.
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
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 01:43 PM
ArrayList ramitmehra123 New To Java 1 02-07-2008 02:47 AM
ArrayList kizilbas1 New To Java 1 01-12-2008 10:48 PM
ArrayList kizilbas1 New To Java 11 12-05-2007 09:30 PM
New to arraylist kleave New To Java 2 11-19-2007 08:45 PM


All times are GMT +3. The time now is 11:28 PM.


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