Bleak Gaming
Bleak Gaming

Reputation: 11

Why can't I properly remove an item from this array?

When I remove an item, I remove it from the array, then I print a message that it has been removed.

I know that since the array is not sorted, the easiest way to remove an item is just to move the current last item in its place, then decrement the number of items.

What did I do wrong? How can I fix it?

public static int removeBall(String[] hookPotentialArray, String[] nameBallArray, int[] ballWeightArray, int count) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter the name of the ball you would like to remove: ");
    String name = keyboard.nextLine();
    boolean ballRemoval = false;
    for (int i = 0; i < count; i++) {
        if (name.compareToIgnoreCase(nameBallArray[i]) == 0 && !ballRemoval) {

            nameBallArray[i] = nameBallArray[count - 1];
            ballWeightArray[i] = ballWeightArray[count - 1];
            hookPotentialArray[i] = hookPotentialArray[count - 1];

            ballRemoval = true;
            count--;

            System.out.println("The ball you selected to be removed "
                    + "has been removed.");
            System.out.println("");
        }
    }
    return count;
}

Upvotes: 1

Views: 81

Answers (2)

Suzan Cioc
Suzan Cioc

Reputation: 30097

(1) you should use break to exit the search

(2) you should add special check when removing last item

(3) you should remember, that java array will not shrink actually, so you need to keep count always correct and in use

like following:

package tests.StackOverflow;

public class q27159679 {

    public static void main(String[] args) {

        String[] hookPotentialArray = {"alpha", "beta", "gamma", "delta"}; 

        String[] nameBallArray = {"A", "B", "C", "D"}; 

        int[] ballWeightArray = {11, 12, 13, 14};

        int count = hookPotentialArray.length;

        printBalls(nameBallArray, count);

        count = removeBall(hookPotentialArray, nameBallArray, ballWeightArray, count, "B");

        count = removeBall(hookPotentialArray, nameBallArray, ballWeightArray, count, "D");


    }


    public static int removeBall(String[] hookPotentialArray, String[] nameBallArray, int[] ballWeightArray, int count, String name) {

        System.out.println("Removing ball " + name);

        for (int i = 0; i < count; i++) {
            if (name.compareToIgnoreCase(nameBallArray[i]) == 0 ) {

                if( i < count - 1) {
                    nameBallArray[i] = nameBallArray[count - 1];
                    ballWeightArray[i] = ballWeightArray[count - 1];
                    hookPotentialArray[i] = hookPotentialArray[count - 1];
                }

                count--;

                System.out.println("The ball you selected to be removed "
                        + "has been removed.");
                printBalls(nameBallArray, count);
                System.out.println("");

                break;
            }
        }
        return count;
    }

    public static void printBalls(String[] nameBallArray, int count) {
        System.out.print("The remaining balls: ");
        for(int i=0; i<count; ++i) {
            System.out.print(nameBallArray[i] + " ");
        }
        System.out.println("");
    }



}

Upvotes: 1

James
James

Reputation: 195

It would be better to create a Ball class to store the names, weights and hook potential values, and use a List to store the Ball objects. Lists also have their own methods to add and remove items.

Upvotes: 1

Related Questions