简体   繁体   中英

How do I delete the last item from a fixed array in java

I am working on an exercise at school. I have to create a cash register and I need to use Arrays. The problem that I have is when I want to delete the last element from the array. For example, the maximum capacity of the array is 42 and I am adding for example items = { 2, 24, 65}.If I print the array I am getting something like output {2,24,65,0,0,0,0,0,0...} until 42.If I am not using the default constructor it seems that my code is working. But if I don`t provide any parameter when creating the object it it takes the default value of the array 42 is not working. I mean I can understand that is deleting the last 0 from the array and it becomes the length of 41 but the item I want to delete is at index 0 1 or 2 depending on how many items I have added. So my question is how do I delete the last item added. For example, if I have 3 items and i call the method 3 times I should have no more items.

import java.util.Arrays;
public class CashRegisterP816
{
    // TODO: insert declarations of instance variables

    private  int itemCount ;
    private int capacity ;
    private double[] items = new double[capacity];    
    /**
     * Constructor for objects of class CashRegisterP816
     * 
     * @param capacity the maximal number of items the cash register can store
     */
    public CashRegisterP816()
    {
       this.items = new double[42];
    }

    /**
     * Constructor for objects of class CashRegisterP816 
     * (with the default capacity)
     */
    public CashRegisterP816(int capacity)
    {

         this.items = new double[capacity];
         this.capacity=capacity;
    }

    /**
     * Gets the number of items in the current sale
     * 
     * @return the item count
     */
    public int getCount()
    {

        return itemCount;

    }

    /**
     * Gets the price of all items in the current sale
     * 
     * @return the total price
     */
    public double getTotal()
    {

        double sum = 0;
        for(double pricePerItem : items){
            sum=sum+pricePerItem;

        }
        return sum;
    }

    /**
     * Clears the cash register before the next sale 
     */
    public void clear() 
    {
        // TODO: 
        itemCount = 0;
        for (int i=0; i<items.length; i++)
        {
        items[i] = 0;
        }
    }    

    /**
     * Adds an item to the cash register
     * 
     * @param price the price of the item
     */
    public void addItem(double price) 
    {  

        if(price > 0 && itemCount < capacity)
        {

         this.items[itemCount] = price;
         itemCount++;
        } 


    }



    /**
     * Cancels the last item in the cash register
     */
        public void cancelLast()
       {
           if(itemCount != 0)
           {
                this.itemCount--;
                this.items=Arrays.copyOf(items, items.length-1);
            }
        }


    /**
     * Prints out all the items in the current sale
     */
    public void displayAll()
    {
        for(int i =0; i<items.length; i++){
        //Print the item array 
        System.out.println(items[i]);
    }

    }    
}

This is the test class. I am getting ok for everything apart from the last instance of the object r3 which is created with the default size of 42.

public class CashRegisterP816Test
{
    public static void main(String[] args)
    {
        CashRegisterP816 r1 = new CashRegisterP816(3);
        r1.addItem(0);
        System.out.println("r1.add(0): " + (r1.getCount() == 0 ? "OK" : "FAIL"));
        r1.addItem(1.2);
        System.out.println("r1.add(1.2): " + (r1.getCount() == 1 ? "OK" : "FAIL"));
        r1.addItem(2.3);
        System.out.println("r1.add(2.3): " + (r1.getCount() == 2 ? "OK" : "FAIL"));
        r1.addItem(-2);
        System.out.println("r1.add(-2): " + (r1.getCount() == 2 ? "OK" : "FAIL"));
        r1.addItem(1.4);
        System.out.println("r1.add(1.4): " + (r1.getCount() == 3 ? "OK" : "FAIL"));
        System.out.println("r1.add(1.4): " + (r1.getTotal() == 4.9 ? "OK" : "FAIL"));
        CashRegisterP816 r2 = new CashRegisterP816(5);
        r2.addItem(3);
        r2.addItem(5.2);
        r2.addItem(6);
        r2.addItem(7);
        System.out.println("r2.add(3, 5.2, 6, 7): " + (r2.getCount() == 4 ? "OK" : "FAIL"));
        System.out.println("r2.add(3, 5.2, 6, 7): " + (r2.getTotal() == 21.2 ? "OK" : "FAIL"));
        r2.addItem(9);
        System.out.println("r2.add(9): " + (r2.getCount() == 5 ? "OK" : "FAIL"));
        System.out.println("r2.add(9): " + (r2.getTotal() == 30.2 ? "OK" : "FAIL"));
        r2.addItem(7);
        System.out.println("r2.add(7): " + (r2.getCount() == 5 ? "OK" : "FAIL"));
        System.out.println("r2.add(7): " + (r2.getTotal() == 30.2 ? "OK" : "FAIL"));
        System.out.println("r1: " + (r1.getCount() == 3 ? "OK" : "FAIL"));
        System.out.println("r1: " + (r1.getTotal() == 4.9 ? "OK" : "FAIL"));
        r1.cancelLast();
        System.out.println("r1.cancelLast: " + (r1.getCount() == 2 ? "OK" : "FAIL"));
        System.out.println("r1.cancelLast: " + (r1.getTotal() == 3.5 ? "OK" : "FAIL"));
        r1.cancelLast();
        System.out.println("r1.cancelLast: " + (r1.getCount() == 1 ? "OK" : "FAIL"));
        System.out.println("r1.cancelLast: " + (r1.getTotal() == 1.2 ? "OK" : "FAIL"));
        r1.cancelLast();
        System.out.println("r1.cancelLast: " + (r1.getCount() == 0 ? "OK" : "FAIL"));
        System.out.println("r1.cancelLast: " + (r1.getTotal() == 0 ? "OK" : "FAIL"));
        r2.clear();
        System.out.println("r1.cancelLast: " + (r2.getCount() == 0 ? "OK" : "FAIL"));
        System.out.println("r1.cancelLast: " + (r2.getTotal() == 0 ? "OK" : "FAIL"));
        CashRegisterP816 r3 = new CashRegisterP816();
        r3.addItem(2.99);
        System.out.println("r3.addItem(2.99): " + (r3.getTotal() == 2.99 ? "OK" : "FAIL"));
        r3.cancelLast();
        System.out.println("r3.cancelLast: " + (r3.getTotal() == 0 ? "OK" : "FAIL"));

        r1.displayAll();
        r2.displayAll();
        r3.displayAll();
    }
}

How do I delete the last item from a fixed array in java

array[array.length-1]=null will set last element to null (or use 0 in case of number array). This will not shrink the array as arrays in Java are always of fixed size

so by default the values which you didn't assign are zero , right?

so why don't you traverse it from back and replace first non-zero number you find with zero, because that non zero number is definitely the last inserted number as you're traversing from back , do it like that

for(int i = yourarray.length ; i >= 0 ; --i)
   {
     if(yourarray[i] != 0) yourarray[i] = 0;
   }

Change this:

   public void cancelLast() {
            // TODO: insert your code here
            if (this.count > 0) {
                this.count--;
            }
        }

and also:

 /** * Clears the cash register before the next sale */ public void clear() { // TODO: insert your code here this.items= null; this.count = 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM