0

I have this array

public static ArrayList<String> inventory = new ArrayList<String>();

and have the players items stored inside of it.

I want to add a feature in the shop class that will sell everything but the pickaxe, how can I create a loop to check if theres something in the array other than "pickaxe" and if there is to remove it?

To remove I have a void

public void removeAllInventory() {

    inventory.clear();

}

or

public void removeInventory(String item) {

    inventory.remove(item);

}

Could I just edit the removeAllInventory to ignore the pickaxe and make a new void called removeAllShop or something? If so what would go in that void?

This is where I need to put it in:

else if  (input.input.equalsIgnoreCase("all")) {


}

4 Answers 4

2

Loop over the list, check if if each element is equal to pickaxe, and remove it if it not.

Iterator<String> i = inventory.iterator();
while (i.hasNext()) {
  if (i.next().equalsIgnoreCase("pickaxe"))
    i.remove()
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that using a for loop without an iterator will result in a java.util.ConcurrentModificationException.
1

You shouldn't edit removeAllInventory() to remove everything but the pickaxe. Its name would no longer make sense, and it seems a reasonable routine to keep around.

But you could add a new method, removeAllInventoryExcept(String item), that removes everything but the given the item.

Hope this helps.

EDIT: In attempt to beef up this answer, I'd also like to suggest the "out of the box" solution of:

public void removeAllInventoryExcept(String item) {
    ArrayList<String> newInv = new ArrayList<String>();
    newInv.add(item);
    inventory = newInv;
}

This avoids the costly iteration and string comparisons.

3 Comments

Is that really worth not being a comment? It does not solve the question.
I'd agree that keeping your code as self documenting as possible is a good way to retain your sanity. Changing the functionality of removeAllInventory starts down the path of making your code...well less than understandable by a 3rd party.
Just as an aside, if the original inventory contained multiple "pickaxe" elements, after calling removeAllInventoryExcept("pickaxe") - there would only now be 1 pickaxe which is why the iteration and string comparison might be required.
0

I'm assuming all of the items in the inventory are an ancestor of an item class, and not just a String.

You could loop through the ArrayList, getting the names of each element and comparing it with the pickaxe name.

or

You could loop through the ArrayList and checking if each item is an instanceof pickaxe, and if it is not, remove it from the ArrayList

edit* It seems you specified the ArrayList is of type String, so ignore the second option

Comments

0
for (String item : inventory) {
   if (!"pixckaxe".equalsIgnoreCase(item)) {
       inventory.remove(item);
   }
}

1 Comment

This code will result in a java.util.ConcurrentModificationException. Do not use!

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.