Results 1 to 3 of 3
  1. #1
    eNine is offline Member
    Join Date
    Aug 2007
    Location
    new yrok
    Posts
    3
    Rep Power
    0

    Default 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();
    }

    }


  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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:
    Java 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();
        }
    }

  3. #3
    eNine is offline Member
    Join Date
    Aug 2007
    Location
    new yrok
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:47 AM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 07:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •