Remove the last value from an array

0

I have a basic college project and one of the requirements of exercise is to remove the last value from an array of integers. I am not able to do with the last value being removed without using an ArrayList, another specification would be to not use an ArrayList and use the Pop function for such an action, I would need some help since the only method I found would be to replace the last one value by 0 and copy this array to a new array, but I think it would not be the most efficient, follow the code with the other functions of the code:

public class Pilha {
    Scanner in = new Scanner(System.in);
    private int top;
    private int[] element = new int[10];
    Pilha(){    
    }

    public int Push(){
            System.out.println("Insira os numeros desejado");
            for(int j = 10; j > 0; j-- ){
                top = in.nextInt();
                element[j-1] = top; 
            }
            return top; 
        }
    public int Pop(){
        return 0;
    }
    public void Show(){
        for(int i = 10; i > 0; i--){
            System.out.println(element[i-1]);
        }
    }
    public void Menu(){
        System.out.println("Digite o que quer fazer");
        System.out.println("Digite 1 para inserir na pilha");
        System.out.println("Digite 2 para remover o  ultimo elemento");
        System.out.println("Digite 3 para mostrar a pilha");
    }
    public void Menu2(){
        System.out.println("Digite o que quer fazer");
        System.out.println("Digite 2 para mostrar a pilha");
        System.out.println("Digite 3 para remover o  ultimo elemento");
    }
    
asked by anonymous 06.06.2017 / 20:44

1 answer

0
As far as I know an Array can not be resized, then the easiest way to get this control is by creating a variable to store which is the last record in the stack.

I used the variable count in the example and looked like this:

import java.util.Arrays;
import java.util.Scanner;

public class Pilha {
    Scanner in = new Scanner(System.in);
    private int top;
    private int count = 0;
    private int[] element = new int[10];
    Pilha(){    
    }

    public void Push(){
            System.out.println("Insira os numeros desejado");
            for(int j = 10; j > 0; j-- ){
                top = in.nextInt();
                element[count] = top;
                count = count + 1;              
            }
            return top; 
        }
    public void Pop(){
        if (count > 0){
            element[count] = 0; //retorna um inteiro nulo para ocupar este espaço
            count = count - 1;
        }
    }
    public void Show(){
        for(int i = count; i > 0; i--){
            System.out.println(element[i-1]);
        }
    }
    public void Menu(){
        System.out.println("Digite o que quer fazer");
        System.out.println("Digite 1 para inserir na pilha");
        System.out.println("Digite 2 para remover o  ultimo elemento");
        System.out.println("Digite 3 para mostrar a pilha");
    }
    public void Menu2(){
        System.out.println("Digite o que quer fazer");
        System.out.println("Digite 2 para mostrar a pilha");
        System.out.println("Digite 3 para remover o  ultimo elemento");
    }
}
    
06.06.2017 / 21:04