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")) {
}
asked Sep 24, 2013 at 21:36

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()
}
answered Sep 24, 2013 at 21:43
1
  • Note that using a for loop without an iterator will result in a java.util.ConcurrentModificationException. Commented Sep 25, 2013 at 22:57
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.

answered Sep 24, 2013 at 21:40
3
  • 2
    Is that really worth not being a comment? It does not solve the question. Commented Sep 24, 2013 at 21:41
  • 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. Commented Sep 24, 2013 at 21:44
  • 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. Commented Sep 24, 2013 at 22:07
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

answered Sep 24, 2013 at 21:43
0
for (String item : inventory) {
 if (!"pixckaxe".equalsIgnoreCase(item)) {
 inventory.remove(item);
 }
}
answered Sep 24, 2013 at 21:45
1
  • This code will result in a java.util.ConcurrentModificationException. Do not use! Commented Sep 25, 2013 at 0:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.