簡體   English   中英

如何將對象添加到ArrayList

[英]How to add an Object to an ArrayList

最近,我更改了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

這是我的語言環境課程:

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

item [i]引用一個Item對象。 如果Item是您的Locale構造函數的參數,我不明白為什么這不起作用。 是否有任何特定的錯誤消息?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM