0

Hello I have a programme where you place tiles to build houses and then you sell the houses. I only want to sell the houses if the building has a door an at least one window. My arraylist looks like this:

   public static ArrayList<block> b = new ArrayList<block>();

every type of tile has an id. the door tile is 1 and the windows tile is 2 and the wall tile is 3.

for(int i = 0; i < play.b.toArray().length;i++){
    if(play.b.contains(play.b.get(i).id == 1)){
        cansell= true;
    }else{
        cansell= false;
    }

}

How can I check to see if an object in an arraylist contains a certain value , in this case the value 1.

here is the door class:

public class door extends block{


    public cockpit(int x,int y,int rot){
        this.x = x;
        this.y = y;
        this.rotate = rot;
        r = new Rectangle(x - (int)play.camx,y - (int)play.camy,20,20);
        id = 3;
    }

    public void tick(){
        createCollisionRect();

        if(Comp.mr && r.contains(new Point((Comp.mx ) ,(Comp.my) ))){
            remove = true;

        }
        if(remove){
            //play.gui.money +=800;

        }

    }

    public void render(Graphics g){
        Graphics2D g2 = (Graphics2D) g;

        if (rotate == 0) {
            ImageIcon i62 = new ImageIcon("res/tiles/cockpit.png");
            img = i62.getImage();
            g.drawImage(img, x - (int) play.camx, y - (int) play.camy,20,20, null);
        }
        if (rotate == 1) {
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            at.rotate(Math.toRadians(90),10,10);

            ImageIcon i62 = new ImageIcon("res/tiles/cockpit.png");
            img = i62.getImage();

            g2.drawImage(img,at, null);
        }
        if (rotate == 2) {
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            at.rotate(Math.toRadians(180),10,10);

            ImageIcon i62 = new ImageIcon("res/tiles/cockpit.png");
            img = i62.getImage();

            g2.drawImage(img, at, null);
        }
        if (rotate == 3) {
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            at.rotate(Math.toRadians(-90),10,10);

            ImageIcon i62 = new ImageIcon("res/tiles/cockpit.png");
            img = i62.getImage();

            g2.drawImage(img, at, null);

        }


    }
}
5
  • So if the list contains the numbers 1 and 2, it can be sold? Commented Apr 19, 2015 at 17:25
  • yes it can be sold no matter what numbers the objects in the arryalist are as long as two of those objects have the id of 1 and 2 Commented Apr 19, 2015 at 17:26
  • 1
    Have you tried if(list.contains(1) && list.contains(2))? Commented Apr 19, 2015 at 17:28
  • play.b.toArray().length can be also written like play.b.size() no need to create a array out of your list Commented Apr 19, 2015 at 17:33
  • Something about this seems...off. What type is play.b, really? Is it Integer or block? Commented Apr 19, 2015 at 17:37

3 Answers 3

1

This seems quite straight forward.

Note there is no need to get "id" ... the ArrayList stores the values itself.

You simply need to check if the index placement value in the array contains the value you are looking for:

for(int i = 0; i < play.b.toArray().length;i++){
    String valueID = play.b.get(i);
    if(valueID.contains("1")){
        cansell= true;
    }else{
        cansell= false;
    }
}

That should answer your question, if not, this really does help: JArrayLists

Hope this answers your question.

Let me know of the outcome

Sign up to request clarification or add additional context in comments.

Comments

0

if(play.b.contains(play.b.get(i).id == 1)){ will evaluate to something like if(play.b.contains(true)){ (or false respectively). So, since you have int in your ArrayList you should change the code following:

cansell = play.b.contains(1);

instead the whole loop.

Comments

0

Per the update in comments:

public void canSell () {
       cansell = play.b.contains(1) && play.b.contains(2);
    }

From: Oracle Java Docs on Lists

2 Comments

If this is a viable approach, then you can reduce this to cansell = play.b.contains(1) && play.b.contains(2);.
Nice! I didn't even think about that. Thanks @Makoto, I updated my answer with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.