【发布时间】:2021-10-04 02:41:52
【问题描述】:
我正在开发一个使用部分填充数组的项目。我在使用空方法时遇到问题。目的是清空一个数组,因此数组的大小应该为零。相反,我的方法将数组的元素更改为等于 0。如果我要打印数组的大小,它会给我一个非 0 的数字。我该如何解决这个问题?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
public class IntBag {
private int capacity;
private String fileName;
private int[] bag;
public void intBag() {
}
public void intBag(int capacity) {
capacity = this.capacity;
}
public void intBag(IntBag other) {
other.capacity = this.capacity;
other.fileName = this.fileName;
other.bag = this.bag;
}
public void empty() {
Arrays.fill(bag, 0);
}
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
IntBag intBag = new IntBag();
int[] bag = new int[20];
intBag.bag = bag;
boolean done = false;
while (!done) {
System.out.println(Arrays.toString(bag));
System.out.println("1. Add an Item to the Array");
System.out.println("2. Replace an Item in the Array");
System.out.println("3. Replace every item in the Array");
System.out.println("4. Look for an Item in the Array");
System.out.println("5. Delete an Item in the Array");
System.out.println("6. Delete all Items in the Array");
System.out.println("7. Size of Array");
System.out.println("8. Empty the Array");
System.out.println("9. Print the Array");
System.out.println("10. Change Length of Array");
System.out.println("11. toString");
System.out.println("12. toStringDescending");
switch (input.nextInt()) {
case 1:
System.out.println(Arrays.toString(bag));
System.out.println("Add an Item to the Array");
intBag.add(input.nextInt());
break;
case 2:
System.out.println(Arrays.toString(bag));
System.out.println("Replace an Item in the Array");
intBag.replace(input.nextInt(), input.nextInt());
break;
case 3:
System.out.println(Arrays.toString(bag));
System.out.println("Replace every item in the Array");
intBag.replaceAll(input.nextInt(), input.nextInt());
break;
case 4:
System.out.println(Arrays.toString(bag));
System.out.println("Look for an Item in Array");
intBag.contains(input.nextInt());
break;
case 5:
System.out.println(Arrays.toString(bag));
System.out.println("Delete an Item in the Array");
intBag.delete(input.nextInt());
break;
case 6:
System.out.println(Arrays.toString(bag));
System.out.println("Delete all Items in the Array");
intBag.deleteAll(input.nextInt());
break;
case 7:
System.out.println("Size of Array: " + intBag.size());
break;
case 8:
System.out.println("Empty the Array");
intBag.empty();
break;
case 9:
System.out.println("Print the Array");
intBag.toArray();
break;
case 10:
System.out.println("Change Length of Array");
intBag.changeCapacity(input.nextInt());
break;
case 11:
System.out.println("toString");
intBag.toString();
break;
case 12:
System.out.println("toStringDescending");
intBag.toStringDescending();
break;
}
}
}
}
【问题讨论】:
-
你需要一个变量来记录当前包里有多少元素。然后,您通过将零分配给袋子来清空袋子。
-
请阅读:How do I ask and answer homework questions?。在寻求帮助之前,您必须真正尝试自己解决编程问题,这一点很重要。编程是需要脑力和毅力的事情。尤其是在你学习的时候。
-
在Java中,数组是不可变的,你不能改变大小。但是您可以为该字段分配一个空数组:
bag = new int[0];(而不是Arrays.fill(bag, 0);)。
标签: java arrays java.util.scanner