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));
    }
}

};

コードの最後の行: map.add(y, new ArrayList(rows)); ArrayList (行) 内のすべての要素に対して Cell のコンストラクター Cell() を実行したいのですが、どうすればよいですか?

4

1 に答える 1

1

リストのリストとその中のすべてのリストを作成します。ただし、実際の Cell オブジェクトではありません。これを試して:

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() );
    }
}
于 2013-03-19T12:12:58.683 に答える