How to exclude an element from a vector in Java

Asked

Viewed 7,492 times

1

I’m having trouble in the case 5, where I have to delete vector elements, can anyone help? I’m starting programming now.

import javax.swing.*;

public class Gere {

    public static void main (String [] arg) {


    Professor Pr[];
    Pr = new Professor [20];

  int Matri = 0;
  int Cont = -1;
  int  Menu = 0;    


 do {  Menu = Integer.parseInt(JOptionPane.showInputDialog("\n1 = Inserir. \n2 = Imprimir. \n3 = Buscar por matricula. \n4 = Buscar  por nome. \n5 = Excluir. \n6 = Sair."));

        switch (Menu) {


    case 1: {
        JOptionPane.showMessageDialog(null,"O menu escolhido foi 1 = Inserir.");    

        Cont ++;

      Pr[Cont] = new Professor();           
      Pr[Cont].setMatricula(Integer.parseInt(JOptionPane.showInputDialog("Informe a Matricula")));
      Pr[Cont].setNome(JOptionPane.showInputDialog("Informe o Nome"));
      Pr[Cont].setSalario(Integer.parseInt(JOptionPane.showInputDialog("Informe o Salario")));
      Pr[Cont].setMateria(JOptionPane.showInputDialog("Informe a Materia"));

    break;
    }   

case 2: {

    JOptionPane.showMessageDialog(null,"O menu escolhido foi 2 = Imprimir.");       

    for (int i = 0; i <=Cont; i++) {

    JOptionPane.showMessageDialog(null,"\n Matricula:" + Pr[i].getMatricula() + 
                                        "\n Nome:" + Pr[i].getNome() + 
                                        "\n Salario:"  + Pr[i].getSalario() +
                                        "\n Materia:" + Pr[i].getMateria() ); 

                                        }

 break; 

 }      

case 3: {

    JOptionPane.showMessageDialog(null,"O menu escolhido foi 3 = Buscar por matricula.");   

    Matri = Integer.parseInt(JOptionPane.showInputDialog("\n Digite a matricula"));


    for ( int e =0; e !=    Matri;  e++) {

    if (Matri == Pr[e].getMatricula() ) {


    JOptionPane.showMessageDialog(null, "\n Matricula:" + Pr[e].getMatricula() + 
                                        "\n Nome:" + Pr[e].getNome() + 
                                        "\n Salario:"  + Pr[e].getSalario() +
                                        "\n Materia:" + Pr[e].getMateria() ); 
    }   
    }   
    break;  
    }       


case 4: {

    JOptionPane.showMessageDialog(null,"O menu escolhido foi 4 = Buscar  por Nome.");   

    String Nom = JOptionPane.showInputDialog("\n Digite o Nome");

    for ( int o=0; o <Cont ; o++) {

    if (Nom.equals(Pr[o].getNome()) ) {


    JOptionPane.showMessageDialog(null, "\n Matricula:" + Pr[o].getMatricula() + 
                                        "\n Nome:" + Pr[o].getNome() + 
                                        "\n Salario:"  + Pr[o].getSalario() +
                                        "\n Materia:" + Pr[o].getMateria() ); 

    }   
    }   
    break;  
    }       

case 5: {
    int remover=0;
    int Matri2 =0;  
    JOptionPane.showMessageDialog(null,"O menu escolhido foi 5 = Excluir.");    

    Matri2 = Integer.parseInt(JOptionPane.showInputDialog("\n Digite a matricula"));

    for ( int u=0; u < Matri ; u++) {

    if (Matri2 == remover(Pr[u].getMatricula()) ) {

    }
    }

    break;  
    }           

    }

    } while (Menu != 6);    


    }

}
  • 1

    Instead of using vector, why don’t you use one List. It is much easier to manipulate objects in it, especially when talking about removal

  • Go he wants to understand still the functioning of vectors...

1 answer

2

  • The quick response is:

You cannot remove elements from a vector. Vectors have defined sizes so there is no way to change their size, what you can do is leave the position x as null, as follows in the example:

Integer[] andaresPredio = new Integer[10];
// preenchendo
andaresPredio [1] = 54;
// apagando valor
andaresPredio [1] = null;

Or you can use ArrayList, I don’t know what your JAVA level so below follows several ways to use the ArrayList and choose the best one for you:

    /** forma 1 */
    List<Integer> numeros = new ArrayList<Integer>();
    numeros.add(15);
    numeros.add(32);
    // removendo
    numeros.remove(1);

    /** forma 2 */
    List numeros2 = new ArrayList();
    numeros2.add(15);
    numeros2.add(32);
    // removendo
    numeros2.remove(1);

    /** forma 3 */
    ArrayList numeros3 = new ArrayList();
    numeros3.add(15);
    numeros3.add(32);
    // removendo
    numeros3.remove(1);

    /** forma 4 */
    ArrayList<Integer> numeros4 = new ArrayList<Integer>();
    numeros4.add(15);
    numeros4.add(32);
    // removendo
    numeros4.remove(1);
  • difference from Arrayls to List is which?

  • List is an interface, ArrayList is a class that implements this interface, if the interface methods already solve what you need it is good to use the interface in the variable signature, but this is convention, in practice the two solve

Browser other questions tagged

You are not signed in. Login or sign up in order to post.