How to get the last item in a list? in java

1

I have a list that I use as a line counter. pressing [ENTER] the list increases by 1 value and I just need to make pressing [BACKSPACE] or delete the last item in the list. and I do not know how to get the last object you know how to do it? This is part of my code

int texto = 11;
int resultado = texto+1;
public void agregarvalores() {
texto = texto + 1;
modelolista.addElement(texto);
}
public void eliminarvalores(){
texto = texto - 1; // la funcion que resta un numero para poder seguir sumando
// AQUI! FALTA la funcion para eliminar el ultimo objeto de una lista
}
private void editorKeyReleased(java.awt.event.KeyEvent evt) {                                   
    if(evt.getKeyCode() == KeyEvent.VK_ENTER){
        //texto++;
        agregarvalores();

    }
    else if(evt.getKeyCode() == KeyEvent.VK_BACK_SPACE ) {
            eliminarvalores();
    }
}     

the values in the table begin at number 11 THANK YOU: v

    
asked by SirFmgames 23.11.2018 в 01:38
source

1 answer

0

To eliminate the last element of your Vector (I see that you use addElement() ) you can use the remove() method and from the measure of the array calculate the index of the last element

System.out.println("Ultimo elemento: "+modelolista.get(modelolista.size()- 1));

To remove the last element:

 modelolista.remove( modelolista.size() - 1);
    
answered by 23.11.2018 / 01:51
source