首页 > 解决方案 > 如何删除数组中的项目并保持顺序不变?

问题描述

我正在做这个项目,它允许我在数组中添加和删除元素。当我删除数组中的元素时,该空间中将有一个零,并且代码应该在删除的值之后移动值以取代它。例如:在数组 {1, 2, 3, 4, 5} 中。我选择删除 3。我的输出应该是 {1, 2, 4, 5}。相反,我的输出是 {1, 2, 5, 4}。有人可以帮我弄清楚为什么会这样做吗?以及如何纠正它?

import java.util.Scanner;
import java.util.Arrays;

public class IntBag2 {
    private static final int INITIAL_SIZE = 20;
    private static int[] bag;
    private int capacity;

    public IntBag2() {
        bag = new int[INITIAL_SIZE];
    }

    public IntBag2(int capacity) {
        bag = new int[capacity];
    }

    public boolean add(int item) {
        if (capacity == bag.length)
            return false;

        bag[capacity++] = item;

        return true;
    }

    public boolean delete(int item) {
        for (int i = 0; i < capacity; i++) {
            if (bag[i] == item) {
                bag[i] = bag[--capacity];
                return true;
            }
        }

        return false;
    }

    @Override
    public String toString() {
        String result = "Bag: ";
        for (int i = 0; i < capacity; i++)
            result += bag[i] + " ";
        return result;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        IntBag2 intBag = new IntBag2();
        boolean done = false;

        while (!done) {
            System.out.println("1. Add an Item to the Array");
            System.out.println("2. Delete an item in the Array");
            System.out.println("3. toString");
            switch (input.nextInt()) {
            case 1:
                System.out.println("Add an Item to the Array");
                System.out.println(intBag.add(input.nextInt()));
                break;
            case 2:
                System.out.println("Delete Item of Array");
                System.out.println(intBag.delete(input.nextInt()));
                break;
            case 3:
                System.out.println("toString");
                System.out.println(intBag.toString());
                break;
            }
        }
        input.close();
    }

}

标签: javaarrayseclipsejava.util.scanner

解决方案


一个更简单的实现delete是允许“删除”数组中等于给定的所有item条目并将剩余的条目有效地移到前面:

public boolean delete(int item) {
    System.out.println("deleting " + item); // for debug purposes
    int oldCapacity = capacity;
    for (int i = 0, j = 0; i < oldCapacity; i++) {
        if (bag[i] != item) {
            bag[j++] = bag[i];
        } else {
            capacity--;
        }
    }
    System.out.println("new capacity = " + capacity); // for debug

    // or use Arrays.fill(bag, capacity, oldCapacity, -1); instead of the loop
    for (int i = capacity; i < oldCapacity; i++) {
        bag[i] = -1; // mark free entries with -1 in the tail
    }

    return oldCapacity == capacity;
}

此外,如果在循环中使用多个连接,toString则应使用方法StringBuilder,或者为简洁起见,可以实现以下print使用实用程序方法的方法:Arrays

public void print() {
    System.out.println(Arrays.toString(Arrays.copyOf(bag, capacity)));
}

测试:

IntBag ibag = new IntBag(10);
ibag.add(1);
ibag.add(3);
ibag.add(3);
ibag.add(2);
ibag.add(1);
ibag.print();
ibag.delete(2);
ibag.print();
ibag.delete(1);
ibag.print();

输出:

[1, 3, 3, 2, 1]
deleting 2
new capacity = 4
[1, 3, 3, 1]
deleting 1
new capacity = 2
[3, 3]

更新

仅“删除”第一个条目而不创建新数组可以实现如下:

  • 跳过所有元素,直到item检测到或bag到达结束
  • 如果item找到,则将剩余元素移 1,将 -1 写入最后一个元素,返回true
  • false否则返回
public boolean deleteFirst(int item) {
    System.out.println("deleting first " + item);
    int id = 0;
    while (id < capacity && bag[id] != item) id++;
    if (id < capacity && bag[id] == item) {
        while (++id < capacity) {
            bag[id - 1] = bag[id];
        }
        bag[--capacity] = -1;
        return true;
    }
    return false;
}

测试:

IntBag ibag = new IntBag(10);
ibag.add(1); ibag.add(3); ibag.add(1); ibag.add(2); ibag.add(1);
ibag.print();
ibag.deleteFirst(1); ibag.print();
ibag.deleteFirst(1); ibag.print();
ibag.deleteFirst(1); ibag.print();

输出:

[1, 3, 1, 2, 1]
deleting first 1
[3, 1, 2, 1]
deleting first 1
[3, 2, 1]
deleting first 1
[3, 2]

推荐阅读