duk*_*vin 1 java syntax arraylist
public class Maze
{
public static final int ACTIVE = 0;
public static final int EXPLORER_WIN = 1;
public static final int MONSTER_WIN = 2;
private Square[][] maze;
private ArrayList<RandomOccupant> randOccupants;
private Explorer explorer;
private int rows;
private int cols;
public Maze(Square[][] maze, int rows, int cols, int numTreasures, int numMonsters, String name)
{
int i;
this.maze = maze;
this.cols = cols;
this.rows = rows;
randOccupants = new ArrayList<RandomOccupant>();
for (i = 0; i < numTreasures; i++)
{
randOccupants.add(i) = new Treasure(this); //COMPILE ERROR
}...
Run Code Online (Sandbox Code Playgroud)
为什么我不能将它添加到arraylist?我相信java文档说我正确地做到了这一点.
你可以这样做:
randOccupants.add( i, new Treasure(this) );
Run Code Online (Sandbox Code Playgroud)
...要么...
randOccupants.add( new Treasure(this) );
Run Code Online (Sandbox Code Playgroud)
两者都是等价的,因为你总是将新元素附加到数组的末尾.
请参阅http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
归档时间: |
|
查看次数: |
1325 次 |
最近记录: |