我有一个arraylist填充了文本文件中的信息,我想删除一个特定的元素

时间:2016-02-19 17:59:23

标签: java arraylist

这是我的代码。我创建了一个方法,搜索arraylist并查看是否有名称匹配。在"删除按钮"部分,我称这种方法。如果有名称匹配,那么我删除该名称和该人的生日。我的问题是程序只会删除arraylist中的第一个元素。如果我尝试删除任何其他元素,我的数组不会更改,元素不会被删除。

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        //cear array
        players.clear();
        //add user input to array
        String name,birthDay;
        name = nameInput.getText();
        birthDay = birthInput.getText();
        playerInfo player;
        player = new playerInfo(name,birthDay);
        players.add(player);
        //add array to next available line in file
        arrayToFile();
    }                                         

    private void exitButonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        //exits program
        System.exit(0);
    }                                         

    private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        outputField.setText("");
        //clear array
        players.clear();

        //add file elements to array
        fileToArray();

        //print array
        outputField.setText(printArray(players));
    }                                          

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        //clear array
        players.clear();
        //add file elements to array
        fileToArray();
        System.out.println(printArray(players));
        //search array and delete player
        String name = nameInput.getText();
        String outputMessage;
        int nameFound = -1;
        nameFound = searchArray(players,name);


        if (nameFound == -1)    {
            outputMessage = "Sorry that name is not in the records.";
        } else {
            players.remove(nameFound);
            outputMessage = name + " has been removed from the records";
        }
        outputField.setText(outputMessage);

        //add updated array to file
        overwriteFile();
    }                                            


    class playerInfo    {
        String name,birthDay;

        playerInfo(String _name,String _birthDay)   {
            name = _name;
            birthDay = _birthDay;
        }
    }

    public void fileToArray() {
        //tempLine will be equal to the information on each line in the file
        String tempLine,name,birthDay;

        //makes sure you don't read past the end of the file
        try {
            //locates the file and opens it to read
            BufferedReader br = new BufferedReader(new FileReader("players.txt"));

            //reads info from file, line-by-line until it sees "null" or "end of file"
            while ((tempLine = br.readLine()) !=null)   {
                //finds the reference numbers of each book
                name = tempLine;
                //finds title name
                tempLine = br.readLine();
                birthDay = tempLine;
                //adds reference number and title of each book into the array
                playerInfo playersAdded;
                playersAdded = new playerInfo(name,birthDay);
                players.add(playersAdded);
            }
            br.close();
        } catch (IOException e) {  
        }
    }

    public void arrayToFile()    {
        Writer output;
        try {
            output = new BufferedWriter(new FileWriter("players.txt",true));
            output.append(printArray(players));
            output.close();
        } catch (IOException ex) {
            Logger.getLogger(projectAgainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void overwriteFile()    {
        Writer output;
        try {
            output = new BufferedWriter(new FileWriter("players.txt"));
            output.write(printArray(players));
            output.close();
        } catch (IOException ex) {
            Logger.getLogger(projectAgainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String printArray(ArrayList <playerInfo> players) {
        String outputMessage = "";
        for (int i=0;i<players.size();i++)  {
            outputMessage += players.get(i).name + "\n" + players.get(i).birthDay + "\n";
        }
        return outputMessage;
    }

    public static int searchArray(ArrayList <playerInfo> players,String nameSearch)  {
        int nameFound = -1;
        String name;
        for (int i=0;i<players.size();i++)  {
            name = players.get(i).name;
            if (nameSearch.equals(name))    {
                nameFound = i;
                return nameFound;
            } else {
                nameFound = -1;
                return nameFound;
            }

        }
        return nameFound;
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton addButton;
    private javax.swing.JLabel ageLabel;
    private javax.swing.JTextField birthInput;
    private javax.swing.JButton exitButon;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton listButton;
    private javax.swing.JTextField nameInput;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextArea outputField;
    private javax.swing.JButton removeButton;
    // End of variables declaration                   
}

2 个答案:

答案 0 :(得分:0)

找到要删除的元素的索引,然后使用它。 ArrayList.remove(int index)方法获取要删除的索引。希望有所帮助! :)

答案 1 :(得分:0)

searchArray 方法更改为:

public static int searchArray(ArrayList <playerInfo> players,String nameSearch)  {
  int nameFound = -1;        
  for (int i=0;i<players.size();i++)  {
    String name = players.get(i).name;
    if (nameSearch.equalsIgnoreCase(name))    {
      nameFound = i;
      break;
    } 
  }
  return nameFound;
}