简体   繁体   English

Java:添加到数组列表

[英]Java: Adding to an array list

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
  }...

Why can't I add this to the arraylist?为什么我不能将它添加到数组列表中? I believe the java docs says that I'm doing this correctly.我相信 java 文档说我这样做是正确的。

You can do either:您可以执行以下任一操作:

randOccupants.add( i, new Treasure(this) );

...or... ...或者...

randOccupants.add( new Treasure(this) );

Both are equivalent, since you're always appending the new element to the end of the array.两者是等价的,因为您总是将新元素附加到数组的末尾。

See https://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html .请参阅https://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html

First, Treasure would need to either inherit from RandomOccupant or implement it (if it is an interface).首先,Treasure 需要从 RandomOccupant 继承或实现它(如果它是一个接口)。

Second, if you want to add it at a particular point in the list, the syntax is其次,如果你想在列表中的特定点添加它,语法是

randOccupants.add(i,new Treasure(this));

Although it's hard to see why you don't just do虽然很难理解你为什么不去做

randOccupants.add(new Treasure(this));

since the items will be added in order even if you don't specify a location.因为即使您没有指定位置,项目也会按顺序添加。

  1. You're trying to add a Treasure to an ArrayList of RandomOccupants, so unless Treasure is a RandomOccupant, that's not going to work.您正在尝试将一个 Treasure 添加到 RandomOccupants 的 ArrayList,因此除非 Treasure 是 RandomOccupant,否则这是行不通的。 The given code does not make it clear whether Treasure is a subclass of RandomOccupant, so it's not possible from the code here to say whether that's part of the problem.给定的代码没有明确说明 Treasure 是否是 RandomOccupant 的子类,因此无法从这里的代码说明这是否是问题的一部分。

  2. What you're actually doing is adding an int to the list here, which is definitely not a RandomOccupant.您实际上在做的是在此处向列表中添加一个 int,这绝对不是 RandomOccupant。

  3. ArrayList's add() method returns either boolean or void depending on the version that you're using, so you can't assign to it. ArrayList 的 add() 方法根据您使用的版本返回 boolean 或 void,因此您无法分配给它。 You're using the method incorrectly.您使用的方法不正确。

The two versions of add() are: add() 的两个版本是:

boolean  add(E e)
void     add(int index, E element)

The second version inserts the element at the specified position, and the first one inserts it at the end.第二个版本在指定位置插入元素,第一个版本在末尾插入元素。 Presumably, what you're intending to do is this:据推测,您打算做的是:

for(i = 0; i < numTreasures; ++i)
    randOccupants.add(new Treasure(this));

But of course, that assumes that Treasure is a subclass of RandomOccupant.但当然,这假设 Treasure 是 RandomOccupant 的子类。 If it isn't, then you'll need to change the type of the ArrayList for it to hold Treasures.如果不是,那么您需要更改 ArrayList 的类型以使其保存 Treasures。

因为它不是 RandomOccupant。

Because you're using the add method wrong.因为您使用的 add 方法错误。 You would call:你会打电话给:

randOccupants.add(new Treasure(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM