I want to remove an index from an array and have tried to do so by copying over all the elements except the one that I want to remove into a new array, however my solution gets ArrayIndexOutOfBoundsException
, what should I do to solve this?
public void removeItem(Item item) {
Item[] ownedItemCopy = new Item[ownedItem.length - 1];
for (int i = 0; i < ownedItem.length; i++) {
if (!ownedItem[i].equals(item)) {
int j = i;
ownedItemCopy[j - 1] = ownedItem[i];
}
}
ownedItem = ownedItemCopy;
}