Results 1 to 2 of 2
  1. #1
    libia is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Vector empty item?

    Hi all,

    I'm new here and I'm new to Java.
    I'm working on a Java project for school. I want to use a Vector to store the positions of the cars, this works very good, until I need to remove a car..
    To save memory en keep it running smooth, I want to remove a car when it rides offscreen, so I build a check like this:
    Java Code:
    if(p.x > 200) {
    	cars.removeElementAt(0);
    }
    p is the Point I got from the vector (using a for-loop starting at int i = 0)
    But once the first car is removed, the applet stops working, no crash or something, the screen just isn't refreshed or something..
    to draw I use this:
    Java Code:
    int size = cars.size();
    for(int i = 0; i < size; i++) {
    	if(cars.get(i) != null) {
    		Point p = (Point)cars.get(i);
    		g.drawRect(p.x, p.y, 4, 2);
    	}
    }
    I searched on google, and they say everywhere, that all items get shifted back when you remove an item from a Vector, so I guess it should work, also the != null check doesn't seem to work...

    I hope you understand my story and that someone can tell me where it goes wrong, if you need more information, just ask please :)

    Sorry for my English btw...

  2. #2
    libia is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    oke, a quick response from me :P

    I seem to have solved it already, but I don't think it's efficient the way I do it...
    instead of removing an item, I do this:
    Java Code:
    cars.set(i, null);
    can anyone tell me how efficient this is?

Similar Threads

  1. tootip for each item (comboBox)
    By justlynn in forum NetBeans
    Replies: 5
    Last Post: 07-29-2010, 03:20 PM
  2. Add item to combobox permanently
    By efozdel in forum New To Java
    Replies: 18
    Last Post: 07-10-2010, 11:30 PM
  3. Add JList item from resultset
    By mine0926 in forum NetBeans
    Replies: 1
    Last Post: 06-17-2010, 09:47 AM
  4. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  5. Remove an item from listbox
    By Dieter in forum Advanced Java
    Replies: 9
    Last Post: 09-21-2009, 10:40 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
  •