Results 1 to 5 of 5

Thread: Arraylist query

  1. #1
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default Arraylist query

    Hi,

    I have an assignment to create an object and add it to an ArrayList of existing objects. The objects from the Arraylist are drawn on a JPanel.

    I need to check the dimensions of the existing objects against the dimensions of the newly created object prior to it being added to the ArrayList.

    I'm unsure how to go about this.

    a brief snippet of my code is:

    XML Code:
    cars = new ArrayList();
    
    Car c = new Car(x, y, dx, dy);
    
            for (int i = 0; i < cars.size(); i++){
    					Car car = (Car)cars.get(i);
    
    // It's here where I am confused. I need to interrogate the x,y values of existing Cars checking that the co-ordinates are not already in use. If so I'll regenerate the co-ordinates and perform the loop again until the new co-ordinates do not clash with any existing object in the ArrayList.
    
    cars.add(c);
    Can someone please point me in the right direction?

    Thanks

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    this is what i've in mind:
    you want to check the bounds of car (in the arraylist), if they clash, then try incrementing the x,y,dx,dy values until you find one that is outside of the bounds....

    that code snippet is not enough to give detailed advice...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    That is exactly what I'm trying to solve. I can't post more code as this is for an assessment. What I'm struggling to achieve is obtaining the individual x,y,dx,dy values of each of the cars in the arraylist to check that they don't clash with the newly created car.

    Is it simply a case of including this in the for loop block to crosscheck the new object with each existing object in the ArrayList:

    if (car.getBounds(x, y, dx, dy) == (c.getBounds(x,y,dx,dy)

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i would write a small prog to test the code.

    for example:
    Java Code:
      public void add(Car c){
        for (int i = 0; i < cars.size(); i++){
          Car d = cars.get(i);
          Point p = c.getLocation();
          
          if(c.intersects(d)){
            p.x += (int)d.getWidth() + 1;
            c.setLocation(p);
          }
        }
        cars.add(c);
      }
    *Car extends Rectangle
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Thanks angryboy - you've given me some food for thought there. Appreciated.

Similar Threads

  1. I need help with a query.
    By Daredemo in forum JDBC
    Replies: 1
    Last Post: 08-13-2008, 06:16 AM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. Help in Query
    By geeta_ravikanti in forum JDBC
    Replies: 0
    Last Post: 03-31-2008, 02:16 PM
  4. Using sql:query tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 04:13 PM
  5. Using sql:query tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 10:31 AM

Posting Permissions

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