9 Replies Latest reply: Aug 5, 2007 7:24 PM by 807605 RSS

    Help ArrayList.add()  --probably silly error

    807605
      I'm relatively new to Java but haven't had any trouble using an ArrayList until trying the class below. Anyone able to tell me what I'm doing wrong?
      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();
      }
      
      }
        • 1. Re: Help ArrayList.add()  --probably silly error
          796440
          What exact problem are you having?
          • 2. Re: Help ArrayList.add()  --probably silly error
            807605
            1) Code tags only work if the code pasted is formated to begin with. Surely all of your code is not left-justified.

            2) ArrayList has to have a valid class within the generic parameter brackets. Rectangle2D.Double doesn�t classify as this. You either have an ArrayList of Double or an ArrayList of Rectangle2D. You need to make up your mind. I think you want just the Rectangle2D if I had to guess.

            3) Always post your full error code.

            Message was edited by:
            petes1234
            • 3. Re: Help ArrayList.add()  --probably silly error
              807605
              Anyone able to tell me what I'm doing wrong?
              It's a lot easier if you tell everyone else what's wrong.

              Does this compile? If not post the message that the compiler gives.

              If it does compile but does not o what you expect, intend or want when you run it, say what it does and what you expected, intended or wanted.
              • 4. Re: Help ArrayList.add()  --probably silly error
                807605
                ArrayList has to have a valid class within the generic parameter brackets.
                Rectangle2D.Double doesn�t classify as this.
                Rectangle2D.Double looks valid enough to me.
                • 5. Re: Help ArrayList.add()  --probably silly error
                  807605
                  The class compiles without an error but I the output from countHats() is


                  There are 0 in list
                  • 6. Re: Help ArrayList.add()  --probably silly error
                    807605
                    Rectangle2D.Double looks valid enough to me.
                    I�ll be danged... that�s what I get for not looking at the API and fully engaging brain before engaging keyboard. I stand corrected (again). Thanks pbrockway2 for the info.
                    • 7. Re: Help ArrayList.add()  --probably silly error
                      807605
                      The class compiles without an error but I the output
                      from countHats() is


                      There are 0 in list
                      that looks like the correct output. h is initiated with the non-parameter constructor and thus doesn�t add any rectangles into its array list. The other Hat objects created in the for loop appear to me to be anonymous objects that are basically discarded as soon as created and are totally independent from h.

                      But given my track record with this thread,... I could be wrong. :(

                      Message was edited by:
                      petes1234
                      • 8. Re: Help ArrayList.add()  --probably silly error
                        807605
                        I�ll be danged...
                        No problem.

                        OP > There are 0 in list

                        And you were expecting 5, right?

                        Just a thought: how many array lists do you think you are creating? In fact, the line
                        ArrayList<Rectangle2D.Double> rectangles = new ArrayList<Rectangle2D.Double>(5);
                        gets executed every time you say "new Hat()". You can get an idea what's going on if you add a line to your two constructors to show what the rectangles array contains:
                        public Hat(){
                            System.out.println("Hat constructor says rectangles=" + rectangles);
                            }
                        
                            Hat(double x, double y, double w, double h){
                                rectangles.add((new Rectangle2D.Double(x,y,w,h) ) );
                                System.out.println("Hat constructor says rectangles=" + rectangles);
                            }
                        
                            // etc...
                        }
                        Do you know what static variables are?
                        • 9. Re: Help ArrayList.add()  --probably silly error
                          796254
                          1) Code tags only work if the code pasted is
                          formated to begin with. Surely all of your code is
                          not left-justified.
                          It appears that it is left-justified. The code tag "worked", it was just badly formatted to begin with.

                          >
                          2) ArrayList has to have a valid class within the
                          generic parameter brackets.
                          It does have a valid class within the generic parameter brackets. Check your javadocs.
                          Rectangle2D.Double doesn�t classify as this.
                          Wrong. Pete doesn't know what he's talking about.
                          You either have an
                          ArrayList of Double or an ArrayList of Rectangle2D.
                          You need to make up your mind. I think you want
                          just the Rectangle2D if I had to guess.
                          Guess again.
                          3) Always post your full error code.
                          The code compiles and runs just fine for me. I'm guessing that the output isn't what you want.

                          I think the problem is that each Hat has its own ArrayList of rectangles. Plus you create an instance h with the default constructor, which leaves its list of rectangles empty. You loop through, do a lot of work, then ask the instance h how many rectangles it has. It tells you the answer is "zero", because that's exactly how many it has.

                          Java is doing what you asked it to do. You just aren't asking what you think you are.

                          %