8 Replies Latest reply: Mar 1, 2009 5:55 PM by 807588 RSS

    Delete elemnet from ArrayList based on search

    630556
      I have a slight problem. I have an action events for each a search and delete button. When search is pressed, input is taken from JTextField, ArrayList is searched, and details returned from the search is printed to a JTextArea on the other side of my gui. Under this JTextArea, there is a delete button, where the user can delete the search result they have just been returned. So i have started of like this.
           private void searchBtn_actionPerformed() {
                
                String lName = lastNameTf.getText();
                passengers = air.getList();
                textArea.setText("");
                
                      for(Passenger a: passengers) {
                           if(a.getLastName().equalsIgnoreCase(lName)) {
                                JOptionPane.showMessageDialog(null, "Passenger Found");
                                textArea.append(a.toString() + "\n");
                                lastNameTf.setText("");
                           }
                           else {
                                JOptionPane.showMessageDialog(null, "Passenger not found");
                                lastNameTf.setText("");
                           }
                      }
           }
           
           private void deleteBtn_actionPerformed() {
                
           }
      Now my search is working fine. But now how can i do a delete on that search from a different Action Event? I think it would be quite silly invoking the search method again, but cant think of any other ways. Any help appreciated.
      cheers
        • 1. Re: Delete elemnet from ArrayList based on search
          807588
          How about during your search method, you keep track of the index of the object being searched for. Then when that search has been completed, store the index of the object somewhere. Then all your delete method need do is remove the object at that index. You will also need some logic that prevents a user from hitting the delete button twice in a row and deleting an object that should not be deleted.
          • 2. Re: Delete elemnet from ArrayList based on search
            807588
            BTW, I'm not fond of your search algorithm. Say you have 5 passengers and the passenger being searched for is the last one. Your code will display "passenger not found" four times before it finds the passenger and displays it's details.
            • 3. Re: Delete elemnet from ArrayList based on search
              630556
              And i can do this by just making a class variable and assigning it a value in the method?
              • 4. Re: Delete elemnet from ArrayList based on search
                807588
                Simply move the searching algorithm to a separate method that returns the index of the found object (-1 if object not found). Then in your first method have an if statement using that returned index.
                • 5. Re: Delete elemnet from ArrayList based on search
                  630556
                  Ok, made a few changes, still having a few difficulties with my delete. So i have my search
                  private int searchMethod(String lastName) {
                            
                            String lName = lastName;
                            passengers = air.getList();
                  
                                 for(int x=0; x<passengers.size(); x++) {
                                      
                                      Passenger singPas = passengers.get(x);
                                      
                                            if(singPas.getLastName().equalsIgnoreCase(lName)) {     
                                            return x;
                                            }
                                 }
                                 
                            return -1;           
                                            
                       }
                  And then i use this in my search action performed method
                       private void searchBtn_actionPerformed() {
                            
                            String lName = lastNameTf.getText();
                            textArea.setText("");
                            int result = searchMethod(lName);
                            
                                  if(result==-1) {
                                      JOptionPane.showMessageDialog(null, "Passenger not found");
                                      lastNameTf.setText("");
                                  } 
                                  else {
                                       JOptionPane.showMessageDialog(null, "Passenger Found");
                                       textArea.append(passengers.get(result).toString() + "\n");
                                       lastNameTf.setText("");      
                                 }
                                  
                       }
                  Now for my delete, i did try making my result variable a class variable, and then done this
                  private void deleteBtn_actionPerformed() {
                            
                            if(result == 0) {
                                 JOptionPane.showMessageDialog(null, "Please search for a Passenger");
                            }
                            else {
                                 passengers.remove(result);
                            }
                       }
                  But it didnt seem to work. I dont know if that is the right approach or if somthing is wrong in the coding? Any advise would be great.
                  cheers
                  • 6. Re: Delete elemnet from ArrayList based on search
                    807588
                    private void searchBtn_actionPerformed() {
                        int result = searchMethod(lName);
                    Not a class variable, just a local to that method.
                    • 7. Re: Delete elemnet from ArrayList based on search
                      630556
                      But then how would i get that index from my search method into my delete method, because that will be the Passenger i need to delete?
                      • 8. Re: Delete elemnet from ArrayList based on search
                        807588
                        By declaring it as a class variable. That way all methods can access it.