Results 1 to 8 of 8
  1. #1
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Doesent delete all the members of arraylist.

    Java Code:
    for(int j = 0; j < particle.size(); j++){
    			if((int)particle.get(j).getX() == (int)mid.getX() && (int)particle.get(j).getY() == (int)mid.getY()){
    				particle.remove(j);
    				if(j < particle.size() -1)
    					j++;
    				}
    			
    			if(particle.get(j).update()){
    				particle.remove(j);
    				if(j < particle.size() -1)
    					j++;
    				}
    
    			}			}
    This code doesent delete all the particles in the array. All particles move torvad mid point, and once t hey reach it they should be deleted, but some still get through. If i take avay the j++ part then this code gives index out of bounds exception. how to make it so, that if particle reaches mid point, it is deleted?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Doesent delete all the members of arraylist.

    Think about what you're doing.

    - In the loop condition you are checking the size() of the collection
    - inside the loop itself, you alter the size of the collection

    Do you see now where the oops is?

    You should use an Iterator (List.iterator()) and its remove() method to safely remove elements from a collection.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Doesent delete all the members of arraylist.

    Iterate in reverse (and do away with the if (...) {... j++ }).
    Java Code:
    for(int j = particle.size() - 1; j >= 0; j--) {
    Think about why that makes a difference.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Doesent delete all the members of arraylist.

    yes i understand it now. Because in eatch loop i might remove a particle andso the lenght of the arrya gets shorter. and if the final member in array is x and i telete it, then it goes x+1 whitch is out of the borders. But if i loop the other way. from x to 0, the length of the array doesent matter, because i take the x-1 member.

    But it still doesent delete all the particles. So it must mean that not all the particles go through the mid point, but move by next to it. Am i right. SO to solve this, i need to increase the area where the particles are deleted. Correct?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Doesent delete all the members of arraylist.

    Its your application, you're the one that is supposed to understand how it functionally works. Its not fair to post a random piece of code and then expect people to understand how it fits in the larger picture without sharing the rest of it as well.

  6. #6
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Doesent delete all the members of arraylist.

    Well, i cant give the full code atm. I have encountered a major crahsdown of my computer. I installed windos on other HDD and while im trying to get this up and runnign properly, i cant get accsess t o the other HDD where has the program on it. But the other parts of the program arent that interesting. It all runs ins a thread. Thread dealy is 10. I have a loop that creates the particles and give them direction to move to. Drawing loop that draws all the particles and a circel and a point where the particles move do. ANd then i have this cycle that handles the movvement of particles and deleting them.

  7. #7
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Doesent delete all the members of arraylist.

    Java Code:
    import java.awt.Image;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import java.util.Random;
    import java.lang.Math;
    
    import java.awt.BasicStroke;
    
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    // Kasutuses siin:
    import java.awt.geom.Ellipse2D;
    //import java.awt.geom.Line2D.Double;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.awt.geom.Ellipse2D.Double;
    
    
    public class Board extends JPanel implements Runnable {
    
        private boolean ingame;
        private int B_WIDTH;
        private int B_HEIGHT;
    	private Thread animator;
    	private Point2D.Double mid; 
    	private ArrayList<Particle> particle;
    	private ArrayList<Line2D.Double> buffer;
    	private Ellipse2D.Double raadius;
    	private Random random;
    	private double dx;
    	private double dy;
    	private int l = 0;
    	
    	private final int DELAY = 100;
    
        public Board() {
            setFocusable(true);
            setBackground(Color.BLACK);
            setDoubleBuffered(true);
            ingame = true;
    		
    		mid = new Point2D.Double(145, 135);
    		particle = new ArrayList<Particle>();
    		raadius = new Ellipse2D.Double(105, 95, 80, 80);
    		buffer = new ArrayList<Line2D.Double>();
    		random = new Random();
    		
    		for(int i = 0; particle.size() < 500; i++){
    			double x = randoms(80) +105;
    			double y = randoms(80) +95;
    			Point2D.Double test = new Point2D.Double(x,y);
    			if(raadius.contains(test)&& x != 145 && y!= 135){
    				double x2 = x - mid.getX();
    				double y2 = y - mid.getY();
    				if(x2 < 0) x2 = x2 * -1;
    				if(y2 < 0) y2 = y2 * -1;
    				if(x < mid.getX() && y < mid.getY()){
    					dx = (x2 < y2)? x2 / y2 : 1;
    					dy = (y2 < x2)? y2 / x2 : 1;
    				}
    				else if(x < mid.getX() && y > mid.getY()){
    					dx = (x2 < y2)? x2 / y2 : 1;
    					dy = (y2 < x2)? (y2 / x2)*-1 : -1;
    					}
    				else if(x > mid.getX() && y < mid.getY()){
    					dx = (x2 < y2)? (x2 / y2)*-1 : -1;
    					dy = (y2 < x2)? y2 / x2 : 1;
    					}
    				else{
    					dx = (x2 < y2)? (x2 / y2)* -1 : -1;
    					dy = (y2 < x2)? (y2 / x2)* -1 : -1;
    				}
    				
    				particle.add(new Particle(x,y,dx,dy,0,0,1,50));
    				}
    			}
    
            setSize(400, 300);
    		
        }
    
        public void addNotify() {
            super.addNotify();
            B_WIDTH = getWidth();
            B_HEIGHT = getHeight();
    		animator = new Thread(this);
    		animator.start();
        }
    	
    	public double randoms(int g){
    		return (random.nextInt(2*g) - g);
    		}
    
        public void paint(Graphics g) {
            super.paint(g);
    		Graphics2D g2 = (Graphics2D) g;
            if (ingame) {	
    			for(int i = 0; i < particle.size(); i++){
    					g2.drawLine((int)particle.get(i).getX(), (int)particle.get(i).getY(), 
    						(int)particle.get(i).getX(), (int)particle.get(i).getY());
    				}
    			g2.drawOval(105,95,80,80);
    			g2.drawLine(145,135,145,135);
            } 
    
            Toolkit.getDefaultToolkit().sync();
            g2.dispose();
        }
    
    	
    	public void cycle() {
    		for(int j = particle.size() -1; j >= 0; j--){
    			if((int)particle.get(j).getX() == (int)mid.getX() && (int)particle.get(j).getY() == (int)mid.getY())
    				particle.remove(j);
    			
    			if(particle.get(j).update())
    				particle.remove(j);
    		}
    	}
    	
    	public void run(){
    		long beforeTime, timeDiff, sleep;
    		
    		beforeTime = System.currentTimeMillis();
    		
    		while(true){
    			cycle();
    			repaint();
    			
    			timeDiff = System.currentTimeMillis() - beforeTime;
    			sleep = DELAY - timeDiff;
    			
    			if(sleep < 0)
    				sleep = 2;
    			try{
    				Thread.sleep(sleep);
    				}catch (InterruptedException e){
    					System.out.println("interrupted");
    				}
    				
    			beforeTime = System.currentTimeMillis();
    		}
    	}
    }
    This is my test program for the particles. Some particles are not deleted when they go through mid point. Is it because some point wont go through but wil go next to it?
    Last edited by vastrolorde; 07-16-2013 at 03:39 PM.

  8. #8
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Doesent delete all the members of arraylist.

    Fixed the problem by changeind the deletion area from 1X1 to 3X3

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  3. Hello to All Members
    By FlyNn in forum Introductions
    Replies: 0
    Last Post: 02-04-2010, 09:53 PM
  4. [newbie] getting class members from Arraylist
    By jon80 in forum New To Java
    Replies: 16
    Last Post: 05-15-2009, 07:45 AM
  5. Replies: 2
    Last Post: 04-20-2009, 08:00 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
  •