0

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.

The inventory of the player is an ArrayList of objects and so is the room's object "sack"

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, i.e , the class which has the main method.

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) ?

-Thanks in advance

1 Answer 1

4

The addAll method will perform the loop for you.

player.inventoryObjects.addAll(room.objects);
Sign up to request clarification or add additional context in comments.

1 Comment

so, is using 2 "."s allowed ? Sorry for asking this question, but please do excuse me as i am a beginner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.