简体   繁体   English

为什么我不能正确地从此阵列中删除一个项目?

[英]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;
}

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. 最好创建一个Ball类来存储名称,权重和潜在值,并使用List来存储Ball对象。 List s also have their own methods to add and remove items. List也有自己的添加和删除项目的方法。

(1) you should use break to exit the search (1)您应使用break退出搜索

(2) you should add special check when removing last item (2)删除最后一项时,应添加特殊检查

(3) you should remember, that java array will not shrink actually, so you need to keep count always correct and in use (3)请记住,java数组实际上不会收缩,因此需要始终保持计数正确并在使用中

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("");
    }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM