0
public class Build_Cells extends Loop {
private List<List<Cell>> map = new ArrayList<List<Cell>>();
public Build_Cells(){
}
public Build_Cells( int height, int width , int cell_size ){
    int col = height / cell_size;
    int rows = width / cell_size;
    for( int y = 0; y < col ; y++){
        map.add(y, new ArrayList<Cell>(rows));
    }
}

};

In the last line of code: map.add(y, new ArrayList(rows)); I want it to run Cell's constructor Cell() for every element in the ArrayList (rows) - but how do I do that?

1 Answer 1

1

You create the list of lists and all the lists in it. But not actual Cell objects. Try this:

for( int y = 0; y < col ; y++){
    List<Cell> colObj = new ArrayList<Cell>(rows);
    map.add(y, colObj);
    for( int x = 0; x < rows ; x++){
        colObj.add( new Cell() );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.