简体   繁体   English

当ArrayList位于对象内部时,访问ArrayList方法

[英]Accessing an ArrayList method, when the ArrayList is inside an object

I am trying to design a text based game. 我正在尝试设计一个基于文本的游戏。

The game will have a feature in which, if the user inputs get all , all the objects in the room must be added to the player's inventory. 游戏将具有一项功能,其中,如果用户输入了all ,则必须将房间中的所有对象添加到玩家的清单中。

The inventory of the player is an ArrayList of objects and so is the room's object " sack " 播放器的清单是对象的ArrayList,房间的对象“ 麻袋 ”也是

The code that i have written is as follows: 我写的代码如下:

public void getAll(Room room, Player player)
  {
      for(int i = 0; i < room.objectNames.size(); i ++)
      {
          player.inventoryObjects.add(room.objects.get(i));
      }
  }

This method is inside the main class, ie , the class which has the main method. 此方法位于主类(即具有main方法的类)内。

The arraylists are declared as: 数组列表声明为:

public ArrayList<Objects> inventoryObjects = new ArrayList<Objects>();
//this is in the Player Class

public ArrayList<Objects> objects = new ArrayList<Objects>();
//this is in the Room Class

Can you tell me an alternative to line: 3 in the first piece of code that i have typed(the one with the loop) ? 您能告诉我另一种替代方法:在我输入的第一段代码(带有循环的代码)中输入3?

-Thanks in advance -提前致谢

addAll方法将为您执行循环。

player.inventoryObjects.addAll(room.objects);

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

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