Char Search in Object Arraylist

Asked

Viewed 123 times

-1

I’m learning java in college and I’m having a hard time developing the logic to search an Arraylist. I want to know if there is a password registered by the type attribute, the passwords were generated with a char(type) value and an int(number) value and concatenated inside the Arraylist. Ex passwords: R-1,P-1,C-1.

I want to search through the Box method1 first on the list, only the passwords started with P respecting the queue, from the first registered to the last, and after all found and deleted, then search by C and R without need of priority, the latter two shall be random according to the position in the queue.

Let me give you another example: Let’s say I add passwords to this sequence in Arraylist: C-1, P-2, C-3, R-4, P-5, P-6, R-7.

Here suppose I want to extract all Arraylist passwords from box 1, but box 1 must prioritize passwords P(Priority) which must follow the following process, remove in the insertion order: P-2, P-5, P-6.

After continuing the removal through the box method1, it should continue and remove in the order that remained without priority: C-1, C-3, R-4, R-7.

Jframe source code to assign type and number to Arraylist.

 private void clienteComumActionPerformed(java.awt.event.ActionEvent evt) {         
        numero++;
        Senha senha = new Senha('C', numero);      
        listaSenhas.add(senha);      
        senhaGerada.setText(String.valueOf(senha.getTipo())+'-'+String.valueOf(senha.getNumero()));
    }                                          

    private void clientePrioritarioActionPerformed(java.awt.event.ActionEvent evt) {   
        numero++;
        Senha senha = new Senha('P', numero);      
        listaSenhas.add(senha);      
        senhaGerada.setText(String.valueOf(senha.getTipo())+'-'+String.valueOf(senha.getNumero()));
    }
           
    private void clienteRapidoActionPerformed(java.awt.event.ActionEvent evt) {        
        numero++;
        Senha senha = new Senha('R', numero);      
        listaSenhas.add(senha);
        System.out.println("lista depois"+listaSenhas);
        senhaGerada.setText(String.valueOf(senha.getTipo())+'-'+String.valueOf(senha.getNumero()));
    }                                            

//Busca, apresentação e remoção  
   
    private void jbcaixa1ActionPerformed(java.awt.event.ActionEvent evt) {             

        saidaCaixa.setText("Caixa 1");      
             
        Iterator<Senha> itr = listaSenhas.iterator();                              
          while(itr.hasNext()){
            Senha s = itr.next();            
            if (s.getTipo() == 'P') {                
            saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));

            itr.remove();
                break;
            }                   
          }
    }                                      

    private void jbcaixa2ActionPerformed(java.awt.event.ActionEvent evt) {             

        saidaCaixa.setText("Caixa 2");
       
        Iterator<Senha> itr = listaSenhas.iterator();
        while(itr.hasNext()) {
            Senha s = itr.next();
            itr.remove();
            saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));
            break;
        }  
    }


public class Senha {
   private char tipo;
        private int numero;
        char contains;

    public Senha() {   // Metodo construtor
        this(' ', 0);  //Esta recebendo os parametros do construtor abaixo
    }
    //Construtor 2
    public Senha (char tipo, int numero) {
        this.setTipo(tipo);
        this.setNumero(numero);         
    }

    public char getTipo() {
        return tipo;
    }

    public void setTipo(char tipo) {
        this.tipo = tipo;
    }

    public int getNumero() {
        return numero;
    }

    public void setNumero(int numero) {
        this.numero = numero;
    }     
}

In this part of the code you have a condition to first display/remove all passwords started in P in the insertion sequence, but I don’t know how to add logic to then be able to display/remove the other C/R passwords in the insertion order.

private void jbcaixa1ActionPerformed(java.awt.event.ActionEvent evt) {             

    saidaCaixa.setText("Caixa 1");      
         
    Iterator<Senha> itr = listaSenhas.iterator();                              
      while(itr.hasNext()){
        Senha s = itr.next();            
          if (s.getTipo() == 'P') {                
            saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));

            itr.remove();
            break;
        }                   
      }

inserir a descrição da imagem aqui

2 answers

0


Here’s an example below, to scroll through your password list again after all preferred passwords have been extracted.

Another way to think about this problem is, when you find a preferred password in your array, and it is removed, in the end only other non-preferential passwords will be present in the array.

You can also create more lists to support this operation, create an extra list for preferred passwords and another list for others. In this case you would use an if/Else, anything that is "P" adds in the preference list, otherwise it is added in the other list. But this way you will be working with 3 lists at the same time, something I believe is unnecessary for your case.

private void jbcaixa1ActionPerformed(java.awt.event.ActionEvent evt) {             

    saidaCaixa.setText("Caixa 1");      
         
    Iterator<Senha> itr = listaSenhas.iterator();                              
      while(itr.hasNext()){
        Senha s = itr.next();            
          if (s.getTipo() == 'P') {                
                saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));
    
                itr.remove();
                break;
 

           }
// uma forma de se fazer isso é criar um novo loop depois de todas as senhas preferênciais terem sido extraídas.
          while(itr.hasNext()){
                Senha s = itr.next();
                    saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));
                    itr.remove();              
              }
  • Thank you for your contribution. Actually I ended up getting hint to use a Return at the end of the first loop, replacing the break;, thus preventing it to execute the second loop in case it did not find the preferred value that was causing error in the code.

0

I switched to a method, replacing the break; at the end of the first loop with a Return;. Thus solved my problem:

private void TestaSenha( String a) {           
        Iterator<Senha> itr = listaSenhas.iterator();           
            while(itr.hasNext()){                    
                Senha s = itr.next();                   
                    if (s.getTipo() == a) {                
                        saidaSenha.setText(String.valueOf(s.getTipo())+'-'+String.valueOf(s.getNumero()));                             
                        itr.remove();
                        System.out.println("lista depois"+listaSenhas);                                        
                            return;
                    }                  
            }
                      
        Iterator<Senha> itr2 = listaSenhas.iterator();
            if (listaSenhas.size()>0) {                                                  
                while(itr2.hasNext()) {
                    Senha r = itr2.next();                                        
                        saidaSenha.setText(String.valueOf(r.getTipo())+'-'+String.valueOf(r.getNumero())); 
                        itr2.remove();
                        System.out.println("itr2"+listaSenhas);
                            break;                                     
                }
            }
    }       

Browser other questions tagged

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