Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Working differently with array elements in the same loop?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Working differently with array elements in the same loop?

    Hey guys. I have a question to see if anyone could help me. I have this classic Asteroid game where asteroids keep falling down and a little ship destroys them with bullets. The asteroids are stored in an ArrayList. There is a for loop in that checks if an asteroid and a bullet collide. If they do, the loop erases the asteroid.

    My problem is that when the bullet does collide one of the asteroids, the hit asteroid does erase, but the other two get deleted as well, even though they show up again quickly. How do I make it so that if a certain element of the array gets hit, only that element gets erased and not the other ones?

    Here is the code in case anyone wants to see it directly.

    		// check bullet-asteroid collisions
    		for(int j = 0; j <= asteroids.size() - 1; j++)
    		{
    			for(int i=0; i<bullets.size(); i++)
    			{
    				Bullet bullet = bullets.get(i);
     
    				if(asteroids.get(j).intersects(bullet))
    				{
    					// increase asteroids destroyed count
    					status.setAsteroidsDestroyed(status.getAsteroidsDestroyed() + 1);
     
    					// "remove" asteroid
    					asteroidExplosion = new Rectangle(asteroids.get(j).x,asteroids.get(j).y,asteroids.get(j).width,asteroids.get(j).height);
    					asteroids.get(j).setLocation(-asteroids.get(j).width, -asteroids.get(j).height);
    					status.setNewAsteroid(true);
    					lastAsteroidTime = System.currentTimeMillis();
     
    					// play asteroid explosion sound
    					soundMan.playAsteroidExplosionSound();
     
    					// remove bullet
    					bullets.remove(i);
    					break;
    				}
    			}
    		}

    Thank you all very much.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,436
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Working differently with array elements in the same loop?

    the hit asteroid does erase, but the other two get deleted as well
    How do you know that two others get deleted? Are you sure that it isn't a problem with the shifting of positions in the list because an element has been removed?
    Try debugging the code by printing out the contents of the ArrayList before and after an item is removed from the list so you can see what is happening to the list.
    If you start at the end of the list and go towards the beginning, removing an element won't shift the position of the elements remaining to be looked at.

    Otherwise: Can you make a small testing program the compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How tp printing array elements vertically through loop.
    By bikes_28 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 6th, 2012, 12:51 PM
  2. How to sum up elements in an array?
    By D-X69 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2012, 01:21 AM
  3. Missing elements in array
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2012, 10:52 PM
  4. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 08:45 AM
  5. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 08:48 PM