java - 如何删除数组中的项目并保持顺序不变?
问题描述
我正在做这个项目,它允许我在数组中添加和删除元素。当我删除数组中的元素时,该空间中将有一个零,并且代码应该在删除的值之后移动值以取代它。例如:在数组 {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();
}
}
解决方案
一个更简单的实现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]
推荐阅读
- java - 添加的 JPanel 仅在从其他方法添加时才显示?
- java - 我可以从 Java 类文件中删除枚举值吗?
- r - 使用 R 将日志收益转换为时间序列预测的实际价格
- javascript - 将 JSON 转换为 GeoJSON 用于 Leaflet 地图的更好方法是什么
- amazon-web-services - 需要更改 Amazon API Gateway 的区域
- java - Arduino 可以从控制台读取字节,但不能从 java
- ios - 连接硬件键盘时保持 UIInputViewController 可见
- python - 无法找到两个日期时间变量之间的差异
- java - 如何检索保存为介质 blob 的多个图像,然后将图像保存到 arrayList 中?
- python - Python“三元组”字典?