删除项目功能在运行时崩溃



作业的要求是:

声明一个 ArrayList 来保存您的 CD 收藏。当按下"初始化"时,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() + "nnnSorted 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");
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集合中。因此,在第二行中,cdss.remove()将获得一个负值作为要删除的元素的索引。

最新更新