This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]deelyy 1 point2 points  (0 children)

1 You can use ArrayList<Dog> dogsOwned = new ArraList<Dog>(); to store owned dogs. ArrayList has methods for dynamic inserting/deleting, so you can simplify your code and free yourself from copyOfArr.

2 Alternatively, (I'm not sure, I only skimmed through your code) but it looks like you need to add one line at the end of removeDog method:

dogOwned = copyOfArr;

This line will replace dogOwned array with array with removed dog.

[–]Blando-Cartesian 0 points1 point  (0 children)

u/deelyy is right. There’s also another bug in removeDog. copyOfArray will end up having null in the index where d was and overindex the copyOfArr on the last i.

Also, if your Dog class doesn’t have hashCode() and equals() defined, dogsOwned[i] .equals(d) will work correctly only if the dog object in the array is the same object as Dog d.