Home > Mobile >  How to empty an array in Java?
How to empty an array in Java?

Time:10-04

I'm working on a project using Partially Filled Arrays. I am having trouble with the empty method. The intention is to empty an array thus the size of the array should be equal to zero. Instead, my method changes the elements of the array to be equal to 0. If I was to print the size of the array it would give me a number other than 0. How can I fix this?

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;
            }

        }
    }

}

CodePudding user response:

Since you said you cannot use ArrayLists, a way to do it with normal arrays would be to simply redeclare the variable.

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

And if you want to re-expand an array size, you could use a method like this to create that array to do the above with.

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;
}

CodePudding user response:

If you don't want to use ArrayList then use List. Here is an example.

public void removeAll(String[]a) {
    java.util.List<String>l = java.util.List.of(a);
    for(int i=0;i<l.size();i  ) {
        l.remove(i);
    }
    
    a = (String[]) l.toArray();
}

CodePudding user response:

Java array sizes are a fixed quantity. That is, once defined, cannot be changed. You may want to use an ArrayList, as you can change the size.

Here would be a sample method to empty array:

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