【问题标题】:How to empty an array in Java?如何在Java中清空数组?
【发布时间】: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


【解决方案1】:

既然你说你不能使用 ArrayLists,那么使用普通数组的一种方法就是简单地重新声明变量。

int[] test = new int[4];
System.out.println(test.length);
test = new int[0];
System.out.println(test.length);
//prints 4 and 0

如果您想重新扩展数组大小,您可以使用类似这样的方法来创建该数组以执行上述操作。

public static int[] resizeArray(int[] oldArray, int newSize){
    int[] newArray = new int[newSize];
    for(int i=0; i<newArray.length && i<oldArray.length; i++){
        newArray[i]=oldArray[i];
    }
return newArray;
}

【讨论】:

    【解决方案2】:

    Java 数组大小是一个固定数量。即一旦定义,就无法更改。您可能想要使用 ArrayList,因为您可以更改大小。

    这是一个清空数组的示例方法:

    public void emptyList(ArrayList<Integer> bag){
        for(int i=bag.size()-1; i>=0; i--){
            bag.remove(i);
        }
    }
    

    【讨论】:

    • 对于这个项目,我不允许使用ArrayLists。
    • Java 垃圾回收意味着清除ArrayList 的最简单方法是分配一个新的空垃圾回收器。如果你必须保留它,bag.clear() 存在并且会这样做,但会留下分配的内部内存,如果你不再使用它,这是浪费的(对于 ArrayList,不是 LinkedList,而是那会慢很多)。
    猜你喜欢
    • 1970-01-01
    • 2010-11-16
    • 2011-04-13
    • 2012-04-05
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多