5 Replies - 3330 Views - Last Post: 09 July 2012 - 05:44 PM Rate Topic: -----

#1 Eldarmk2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 08-July 12

Delete object from ArrayList

Posted 09 July 2012 - 01:25 AM

Hi guys, im looking for an solution how to delete an object in an ArrayList if a parameter of it fits a certain criteria.

For example if i have an ArrayList with 3 objects inside them the got a int var, i need to delete those objects whos var==1

How can i do that ?

Example:

Object.class
public class Object {
int var;
	
public void Object (int var){
		this.var=var;
	}
}



Arrays.class

import java.util.ArrayList;
import java.util.List;


public class Arrays {
List <Object> l = new <Object> ArrayList();

public Arrays(){
	l.add(new Object(1));
	l.add(new Object(2));
	l.add(new Object(1));
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Delete object from ArrayList

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Delete object from ArrayList

Posted 09 July 2012 - 01:41 AM

Use an Iterator of your list and an if statement to determine if the number is within your condition to be removed.

Ex:
Iterator<Object> it = l.iterator();
		while(it.hasNext()){
			Object o= it.next();
			if(o.var==3){
				it.remove();
			}
		}



By the way why are you using the Object class? Couldn't you create a more descriptive class, not the superman of all classes?
Was This Post Helpful? 0
  • +
  • -

#3 Eldarmk2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 08-July 12

Re: Delete object from ArrayList

Posted 09 July 2012 - 02:17 AM

Oh yes, i use the iterator but in other case.
Let me explain a bit, i try to make a puyo-puyo game, tetris like.
The goal is to delete spheres that have a same color and are more then 3 in a row.
The object class was just an example, i have in my game a Sphere class.
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Delete object from ArrayList

Posted 09 July 2012 - 02:25 AM

Now what is the problem? I guess the if statement will cover your removing part. :)
Was This Post Helpful? 0
  • +
  • -

#5 Eldarmk2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 08-July 12

Re: Delete object from ArrayList

Posted 09 July 2012 - 04:54 AM

welll ok ive posted my code already in an other thread but, here it is again.
Panel.class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.Timer;


public class Panel extends JPanel{
	Timer timer1,timer2;

	Timer inputTimer;

	int [][] grid =	new int [12][7];

	int vSpeed=200;

	int dy=0;

	List<Shape> spheres = new ArrayList<Shape>();	
	
	ArrayList toDelete = new ArrayList<Integer>();

	private boolean spawn = false;
	
	private boolean removeVertical;

	private boolean removeHorizontal;
	
	private boolean left;
	
	private boolean right;	

	int objIndex;

	int SpheresAmount;
	
	
//----------------------- The panel constructor
	public Panel ()  
	{
		//Init JPANEL 
		setFocusable(true);
		setBackground(Color.BLACK);
		setDoubleBuffered(true);
		addKeyListener(new MyKeyAdapter());
		timer1 = new Timer(vSpeed,new ListenerTimer1());
		timer2 = new Timer (100,new ListenerTimer2());
		timer1.start();
		timer2.start();
		spheres.add(new Shape());
	}
	
/* ------------------  Main Method------------------*/
	
	public void paint (Graphics g)
	{
		super.paint(g);
		Graphics2D g2d= (Graphics2D) g;
		Iterator<Shape> j = spheres.iterator();
		
		while (j.hasNext())
		{
			Shape s = j.next();	
			g.drawImage(s.getImage(),s.getY(),s.getX(),null	);
			
		//-------Border Collision check-------//
			if (grid[s.getGx()+1][s.getGy()]!=0||s.getGx()==10)
			{	
				if (!j.hasNext())
				{
					s.setDx(0);
					grid[s.getGx()][s.getGy()]=s.getColor();
					spawn=true;
					
				}			
			}
		//-------3 vertical check-------//
			if (grid[s.getGx()+1][s.getGy()]==s.getColor()&&grid[s.getGx()-1][s.getGy()]==s.getColor())
			{
				SpheresAmount=spheres.size();
					removeVertical=true;
			}
		//-------3 horizontal check---
			if (grid[s.getGx()][s.getGy()+1]==s.getColor()&&grid[s.getGx()][s.getGy()+2]==s.getColor())
				removeHorizontal=true;
	
			
		
		//--------END LOOP-------//
		}
			if (removeVertical)
			{
				deleteVertical();				
				removeVertical=false;
			}
		
			if (removeHorizontal)
			{
				deleteHorizontal();
				removeHorizontal=false;
			}
		
			if(spawn)
			{
				spheres.add(new Shape());
				spawn = false;
			}
	}

	

	private void deleteHorizontal()
	{
		Iterator<Shape> j = spheres.iterator();
		while (j.hasNext()){
			Shape d = j.next();
			
			
			if (spheres.size()+2<SpheresAmount){
				grid[d.getGx()][d.getGy()]=0;
				grid[d.getGx()][d.getGy()+1]=0;
				grid[d.getGx()][d.getGy()+2]=0;
				SpheresAmount=spheres.size();
			}
		}
		
	}

	
	private void deleteVertical() {
		Iterator<Shape> j = spheres.iterator();
		while (j.hasNext()){
			Shape d = j.next();
			if (grid[d.getGx()+1][d.getGy()]==d.getColor()&&grid[d.getGx()+2][d.getGy()]==d.getColor()){j.remove();}
			if (grid[d.getGx()-1][d.getGy()]==d.getColor()&&grid[d.getGx()+1][d.getGy()]==d.getColor()){j.remove();}
			if (grid[d.getGx()-1][d.getGy()]==d.getColor()&&grid[d.getGx()-2][d.getGy()]==d.getColor()){j.remove();}
			if (spheres.size()+2<SpheresAmount){
				
				grid[d.getGx()][d.getGy()]=0;
				grid[d.getGx()+1][d.getGy()]=0;
				grid[d.getGx()+2][d.getGy()]=0;
				SpheresAmount=spheres.size();
			}
			
		}
		
	}

	// ---------------- INPUT -------------------//
	private class MyKeyAdapter extends KeyAdapter
	{
		
		public void keyReleased (KeyEvent e){
			int	key = e.getKeyCode();
			if(key==KeyEvent.VK_LEFT)left=false;
			if(key==KeyEvent.VK_RIGHT)right=false;
			
		}
		public void keyPressed (KeyEvent e){
			
			int	key = e.getKeyCode();
			if(key==KeyEvent.VK_LEFT)left=true;
			if(key==KeyEvent.VK_RIGHT)right=true;
			
		}
	}
	
	// ------------Action Listener---------------//
	private class ListenerTimer1 implements ActionListener
	{

		public void actionPerformed(ActionEvent arg0) 
		{
			Iterator <Shape> j  = spheres.iterator();
			while (j.hasNext())
			{
				Shape s = j.next();
				s.move();
			}
			
			repaint();
			}
		
	}
	// ------------Input movement updater-----------//
	private class ListenerTimer2 implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{
			
			Iterator <Shape> j  = spheres.iterator();
			while (j.hasNext())
			{
				Shape s = j.next();	
				if (s.dx!=0&&right==true){
					s.moveRight();repaint();
				}
				if (s.dx!=0&&left==true){
					s.moveLeft();repaint();
				}
			}
		}
		
	}
	//-----------------------------------------------//
}


Hope you can read my code, ill explain every line if needed.
As you can see the deletehorizontal method is doing nothing this becasue ive deleted the crap that i had there.
The deletevertical is actually working, but i think ive gone a wrong way with this checks and now im stucked.
If the lissting for the Shape.class is required ill post it.
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Delete object from ArrayList

Posted 09 July 2012 - 05:44 PM

Noooo! It's Java GUI! I'm afraid I do not know anything about this. Basically your emptying of arraylist occurs if you set the array value as 0? Why not remove that array value altogether?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1