2017-09-23 62 views
-3

我有一个电池类作为访问ArrayList的一个细胞的特定属性在Java中

public class cell { 
private boolean pointerIsHere; 
private boolean hurdle; 
private boolean startCell; 
private boolean endCell;} 

现在我在另一个类grid使这个细胞类型的双ArrayList中。 这个ArrayList的初始化是;

gameGrid(int rows,int columns){ 
    this.rows=rows; 
    this.columns=columns; 
    this.grid=new ArrayList<List<cell>>(this.rows); 

    for(int i=0; i<columns; i++) { 
     this.grid.add(new ArrayList<cell>(this.columns)); 
    } 
} 

grid类的方法,我需要设置startCell作为ArrayList中的特定小区的true。我应该如何访问特定的单元格及其属性?

+0

写的制定者。 –

+1

什么是double ArrayList? –

+0

@ThorbjørnRavnAndersen我做到了。在'cell'类中。问题是访问double arrayList中的特定单元的setter。 – AghaKhan

回答

2

由于该字段是私人的,您将不得不实施getter/setter方法。然后你使用嵌套列表get()方法:

public class cell { 
    // ... 
    public void setStartCell(boolean start) { 
     this.startCell = start; 
    } 
} 

而对于接入小区的二传手在列表中:

ArrayList<ArrayList<Cell>> l = new ArrayList<>(); 
// list filling 
l.get(row).get(col).setStartCell(true); 
+0

phew。那很简单 – AghaKhan