我正在研究這個專案,它允許我添加和洗掉陣列中的元素。當我洗掉陣列中的元素時,該空間中會有一個零,代碼應該在洗掉的值之后移動值來代替它。例如:在陣列 {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();
}
}
uj5u.com熱心網友回復:
一個更簡單的實作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]
uj5u.com熱心網友回復:
通過該行,bag[i] = bag[--capacity];您基本上可以獲取陣列中的最后一項并將其放置在已洗掉項的位置。
鑒于您正在使用int[](而不是Integer[]),我們無法將陣列的索引分配為null。我們能做的最好的事情就是分配它-1或創建一個新陣列。
我決定從頭開始創建一個新陣列。以下將解決問題。
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) {
int[] newBag = new int[capacity];
int newCapacity = capacity;
boolean deleted = false;
for (int i = 0, j = 0; i < capacity; i ) {
if (bag[i] == item && !deleted) {
deleted = true;
newCapacity = capacity - 1;
} else {
newBag[j ] = bag[i];
}
}
bag = newBag;
capacity = newCapacity;
return deleted;
}
@Override
public String toString() {
String result = "Bag: ";
for (int i = 0; i < capacity; i )
result = bag[i] " ";
return result;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324399.html
標籤:爪哇 数组 蚀 java.util.scanner
