Add objects to Arraylist inside loop and get a list of them outside loops

Posted by AgusDG on Stack Overflow See other posts from Stack Overflow or by AgusDG
Published on 2012-12-17T16:53:37Z Indexed on 2012/12/17 17:03 UTC
Read the original article Hit count: 269

Im already done with a method to do a shot on a board (bidimensional array). THe shot goes from the bottom to the top, and depending of the direction, it do bounces on the walls to get to the top.

The thing is that I did the method to represent the trayectory with an 'x'. Now, I want to add the coordinates x and y of each position of the shot (b [x][y]) to and Arraylist of Objects Position.

public Position(int row,int col) {
            this.row = row;
            this.col = col;
        }

The thing is that the method uses a for loop and inside if loops, and I'll need to create the objects inside, and get them outside. I did that :

    public static ArrayList<Position> showTrayectory (char [][] b , int shotDirection, char bubble){
        int row = 0, col = 0;
        ArrayList<Position> aListPos = new ArrayList<Position>();
        Position positionsOfShot = new Position(row,col);
        START = ((RIGHT_WALL)/2) + shotDirection;
        boolean shotRight = false;
        if(shotDirection < 0)
            shotRight = false;
        else if(shotDirection > 0)
            shotRight = true;
        for(int y = BOTTOM,x = START ;y >= 0;y--) {
            if(!isOut(y,x) && !emptyCell(y,x)) break;
            if(x <= LEFT_WALL) 
                shotRight = true;
            if(x >= RIGHT_WALL) 
                shotRight = false;
            if(!isOut(y,x) && shotRight == true) {
                positionsOfShot = new Position(y,x);
                aListPos.add(positionsOfShot);
                b[y][x] = SHOT;
                ++x;
            }
            if(!isOut(y,x) && shotRight == false){
                positionsOfShot = new Position(y,x);
                aListPos.add(positionsOfShot);
                b[y][x] = SHOT;
                --x;
            }
        }
                   // The nested for loops below are for showing the positions
                   // But I dont need it that way
                   // I must get the trayectory from an ArrayList and print it from there                                    
        for(int y=0;y < b.length;y++){
            System.out.println();
            for(int x=0;x < b[y].length;x++){
                System.out.print(" "+b [y][x]+" ");
            }       
        }
        System.out.println("\nTrayectory of the shot ["+shotDirection+"]");
        System.out.println("Next bubble ["+bubble+"]");

        for( Position ii : aListPos){
            System.out.println("(" + positionsOfShot.getFila() + "," + positionsOfShot.getColumna()+")");
        }
        return aListPos;
    }

The sentence " b[y][x] = SHOT; " is still there, to see the proper trayectory of the shot (its not needed that way), but what I need, is getting the trayectory in an ArrayList, and print the trayectory from there.

All that I get is a wrong position, and repeated during the number of positions the shot goes through.

I need some help. I suppose the problem is that Im creating and adding Position Objects inside an ArrayList inside loops, but in a wrong way. I will need you to explain me how to do it properly ; )

Thanks in advance.

I'll add the output for you see better what is that above haha

***************************

 y  b  y  b  g  r  b  g  o 
 y  g  a  a  r  y  o  y  y 
 r  b  y  g  r  r  o  b  o 
 y  y  g  b  a  r  y  r  o 
 a  y  y  o  o  r  r  g  r 
 -  -  -  x  -  -  -  -  - 
 -  -  -  -  x  -  -  -  - 
 -  -  -  -  -  x  -  -  - 
 -  -  -  -  -  -  x  -  - 
 -  -  -  -  -  -  -  x  - 
 -  -  -  -  -  -  -  -  x 
 -  -  -  -  -  -  -  x  - 
 -  -  -  -  -  -  x  -  - 
 -  -  -  -  -  x  -  -  - 
Trayectory of the shot [1]
Next bubble [y]
(5,3)
(5,3)
(5,3)
(5,3)
(5,3)
(5,3)
(5,3)
(5,3)
(5,3)

Action?

© Stack Overflow or respective owner

Related posts about java

Related posts about object