Results 1 to 4 of 4
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default How do I make an array of objects and delete an entire array of objects?

    Hi I am trying to delete entire set of objects from my graphics program. Specifically I am trying to make a bunch of tree trunks, 10 in total, and then delete them all by pressing a button.
    I can manage to make the trunks but I can only delete one of them.
    Below I can walk you through some of my code step by step to show you what I am trying to do:

    Java Code:
    public void drawTrunk(double width, double height, double xpos, double ypos) {
            trunks = new ArrayList(10);
            trunk = new GRect(width / 15.0, height / 3.0);
            trunk.setFilled(true);
            trunk.setFillColor(Color.orange);
    //this is where I'm trying to make an array of trunks
            for(int i=0; i<=10; i++){
            trunks.add(trunk);    
            } 
            add(trunk, xpos + width / 2.0 - width / 2.8, ypos + height / 6.0); 
    //one trunk is added but the next method draws the rest
        }
    
    public void drawBackGround() {
            drawSky();  //not important right now
            drawGrass(1.0 / 2.0);  //not important right now
            for (int xpos = 10; xpos < getWidth() - 40; xpos += getWidth() / 10) {
                drawTrunk(150, 200, xpos, getHeight() / 2.0 - 50);  //makes multiple tree trunks
            }
    
    //this is where I try to clear all the trunks
        public void clearActionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getActionCommand().equals("Clear")) {
                for (Iterator<GRect> it = trunks.iterator(); it.hasNext();) {
                    GObject gobj = it.next();
                   this.remove(gobj);  //removes just one tree trunk
               }
           }
        }
    So basically I just would like to know how to make this work? Thanks

  2. #2
    Matrice is offline Member
    Join Date
    Jul 2014
    Location
    Mililani
    Posts
    37
    Rep Power
    0

    Default Re: How do I make an array of objects and delete an entire array of objects?

    Let me see...
    int [] ArExample = {22, 21, 20};

    Here it is declared and is initialized. (Hope this is the sort of formula you're looking for)

    I think that your program is wonderful. I like those trunk variables!

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default Re: How do I make an array of objects and delete an entire array of objects?

    just put println statement afer adding trunk.I guess only one object got stored...not 10 i guess
    Ramya:cool:

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How do I make an array of objects and delete an entire array of objects?

    My crystal ball tells me you get an exception on the second time round the loop if there's more than one of these things. A ConcurrentModificationException.

    In any case, Lists have a method for clearing them, so you don't have to iterate over the list and remove each element individually.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 04:48 PM
  2. help with and array of objects
    By hoosierfan24 in forum New To Java
    Replies: 5
    Last Post: 01-23-2011, 03:45 AM
  3. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 05:03 AM
  4. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 07:27 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
  •