运行时删除项目功能崩溃

时间:2017-04-28 20:00:27

标签: java

作业的要求是:

  

声明一个ArrayList来保存你的CD集合。按下Initialize时,ArrayList将填充5个CD标题。然后,“初始化”按钮将被禁用,其他按钮将变为启用状态。您的计划应该能够:

     
      
  • 显示CD标题列表。
  •   
  • 按字母顺序对CD标题进行排序。
  •   
  • 添加新CD标题。
  •   
  • 用户可以在输入框中输入标题CD,然后按“删除”按钮从列表中删除CD。
  •   

我特意在删除功能方面遇到问题。每当我尝试从集合中删除一首歌时,它就会崩溃。提前谢谢!

public class CDCollection extends javax.swing.JFrame {

    // Creating array in public class so it can be used through the entire project
    // The extra s in cdss stands for sorted
    ArrayList <String> cds = new ArrayList();
    ArrayList <String> cdss = new ArrayList();
    // Random number generator
    Random rn = new Random();

    /**
     * Creates new form CDCollection
     */
    public CDCollection() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        lblTitle = new javax.swing.JLabel();
        lblTitleArtist = new javax.swing.JLabel();
        txtTitleArtist = new javax.swing.JTextField();
        btnDisplay = new javax.swing.JButton();
        btnInitialize = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnrRemove = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtaDisplay = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblTitle.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
        lblTitle.setForeground(new java.awt.Color(0, 0, 153));
        lblTitle.setText("CD Collection");

        lblTitleArtist.setText("Title - Artist");

        btnDisplay.setText("Display");
        btnDisplay.setEnabled(false);
        btnDisplay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisplayActionPerformed(evt);
            }
        });

        btnInitialize.setText("Initialize");
        btnInitialize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInitializeActionPerformed(evt);
            }
        });

        btnAdd.setText("Add");
        btnAdd.setEnabled(false);
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnrRemove.setText("Remove");
        btnrRemove.setEnabled(false);
        btnrRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnrRemoveActionPerformed(evt);
            }
        });

        txtaDisplay.setEditable(false);
        txtaDisplay.setColumns(20);
        txtaDisplay.setLineWrap(true);
        txtaDisplay.setRows(5);
        txtaDisplay.setWrapStyleWord(true);
        jScrollPane1.setViewportView(txtaDisplay);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(lblTitleArtist)
                        .addGap(44, 44, 44)
                        .addComponent(txtTitleArtist, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 489, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnDisplay)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnInitialize)
                                .addGap(109, 109, 109)
                                .addComponent(btnAdd)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnrRemove)))))
                .addContainerGap(16, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(lblTitle)
                .addGap(176, 176, 176))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(lblTitle)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTitleArtist)
                    .addComponent(txtTitleArtist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDisplay)
                    .addComponent(btnInitialize)
                    .addComponent(btnAdd)
                    .addComponent(btnrRemove))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

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

        // Add CDs to both arrays
        Collections.addAll(cds, "Metric - Fantasies", "Beatles - Abbey Road", "Pearl Jam - Ten", "Doors - Alive", "The Rolling Stones - Gimme Shelter");
        Collections.addAll(cdss, "Metric - Fantasies", "Beatles - Abbey Road", "Pearl Jam - Ten", "Doors - Alive", "The Rolling Stones - Gimme Shelter");

        // Display original five CDs in the collection
        txtaDisplay.setText("Songs in collection");
        int condition = 0;
         while(condition < cds.size()){
        txtaDisplay.setText(txtaDisplay.getText() + "\n" + cds.get(condition));
        condition++; }

        // Disable initialize button and enable all other buttons
        btnInitialize.setEnabled(false);
        btnDisplay.setEnabled(true);
        btnAdd.setEnabled(true);
        btnrRemove.setEnabled(true);

    }                                             

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

        // Display collection
        txtaDisplay.setText("Original Order");

        int condition = 0;
    // Condition is the value that is increased by 1 every time each of these while loops are ran 
    // From 1 to however many objects there are in the either the CDS or CDSS array list
    while(condition < cds.size()){
        txtaDisplay.setText(txtaDisplay.getText() + "\n" + cds.get(condition));
        condition++; }

    txtaDisplay.setText(txtaDisplay.getText() + "\n\n\nSorted Order");

    // This sorts the cdss array in alphabetical order
    Collections.sort(cdss, String.CASE_INSENSITIVE_ORDER);
    condition = 0;

    // The same as the previous while loop
    while(condition < cdss.size()){
        txtaDisplay.setText(txtaDisplay.getText()+ "\n" + cdss.get(condition));
        condition++;
    }


    }                                          

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

    //Retrieving user data
    String inputSong = txtTitleArtist.getText();

    // If statement allows users to add songs and determines if the song the 
    // user is trying to enter is already in the collection
    if(Collections.binarySearch(cdss, inputSong) < 0){
        cds.add(inputSong);                             
        cdss.add(inputSong);                            
        Collections.sort(cdss); 
    } else {
        txtaDisplay.setText("This song is already in the collection.");
    }


    }                                      

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

        // Retrieve user data
        String inputSong = txtTitleArtist.getText();

        // if the inputted song is in the array this line will run
        if(Collections.binarySearch(cdss, inputSong) > -1){
        cds.remove(Collections.binarySearch(cds, inputSong));
        cdss.remove(Collections.binarySearch(cds, inputSong));
        } else {
            txtaDisplay.setText("That is not a song in the collection");

1 个答案:

答案 0 :(得分:0)

List((12345,IntegerType), (fnord,StringType))
List((42,IntegerType), (baz,StringType))

问题来自第二行。在cds.remove(Collections.binarySearch(cds, inputSong)); cdss.remove(Collections.binarySearch(cds, inputSong)); 中,您使用变量Collections.binarySearch(cds, inputSong)而不是cds,因为在第二行中您要从cdss删除。 事实上,在第一行之后,cdss将返回负值,因为Collections.binarySearch(cds, inputSong)不再位于inputSong集合中。因此,在第二行中,cds将获得负值作为要删除的元素的索引。