如何将对象添加到ArrayList

时间:2016-03-11 20:33:40

标签: java adventure

我最近改变了我的Locale构造函数,以获取一个名为Item的数组而不仅仅是一个项目名称,现在我在游戏中使用某个项目时遇到了一些困难。这是一款文字冒险游戏,玩家可以在地图上移动并拾取房间内的物品。我收到错误:"类型ArrayList中的方法add(String)不适用于参数(Item)"。我不确定如何解决这个问题。

感谢您的帮助!

这是我的代码:

static int currentLocation = player1.currentRoom;

//Items {itemName, itemDes}
 static Item[] items = {
         new Item ("map","A layout of your house."),
         new Item ("battery", "A double A battery."),
         new Item ("flashlight", "A small silver flashlight."),
         new Item ("key", "This unlocks some door in your house."),

 };

//Locations {roomName, description, Item}
 static Locale[] locales = { 
         new Locale("bedroom","You see the outline of a bed with your childhood stuffed bear on it.",items[0]),
         new Locale("hallway","A carpeted floor and long pictured walls lie ahead of you.",null),
         new Locale("kitchen","The shining surface of your stove reflects the pale moonlight coming in the window over the sink.",items[1]),
         new Locale("bathroom","You find yourself standing in front of a mirror, looking back at yourself.",items[2]),
         new Locale("living room","You stub your toe on the sofa in the room, almost falling right into the TV.",null),
         new Locale("dining room","You bump the china cabinet which holds your expensive dishes and silverware.",items[3]),
         new Locale("office","The blinking light from the monitor on your desk can be seen in the dark",null),
         new Locale("library","The smell of old books surrounds you.",null),
         new Locale("basement","You reach the top of some stairs and upon descending down, you find the large metal generator.",null),   
 };

//Take
          else if(response.equalsIgnoreCase("T")){
              Locale locale = locales[currentLocation];
              // check if the locale has an item.
              if(locale.item != null){
                  // create an "ArrayList" with all the items from the players inventory or an empty one if the inventory is "null"
                  ArrayList<String> inventory;
                  if(player1.inventory != null){
                      inventory = new ArrayList<String>(Arrays.asList(player1.inventory));
                  }
                  else{
                      inventory = new ArrayList();
                  }
                  // add the item to the list and set the list as the player's inventory by converting it to an array.
                  inventory.add(locale.item);
                  player1.inventory = inventory.toArray(new String[inventory.size()]);
                  System.out.println("\tA " + locale.item + " was added to the inventory");
                  System.out.println("\n\tYou can view your inventory by pressing 'I'.");
                  System.out.println("\n\tFive points have also been added to your score.");
                  player1.score += 5;
                  System.out.println("\n\tThis is your current score: "+player1.score);
                  locale.item = null;
              }
              // this locale doesn't have an item.
              else{
                  System.out.println("\tThere is no item to pick up");
              }
              break;
          }//End of Take

这是我的Locale类:

public class Locale {

//Locale must have name, description, and Item
public static int roomNumber;
public String roomName;
public String description;
public Item item;


public Locale(String roomName, String description, Item item){
    this.roomName = roomName;
    this.description = description;
    this.item = Item;
}
}

这是我的Item类:

public class Item {
//item must have a name and a description (both strings)
public String itemName;
public String itemDes;

public Item (String itemName, String itemDes){
    this.itemName = itemName;
    this.itemDes = itemDes;
}
}

1 个答案:

答案 0 :(得分:0)

item [i]引用Item对象。如果Item是您的Locale构造函数的参数,我不明白为什么这不起作用。是否有任何特定的错误消息?