将int添加到arrayList

时间:2015-06-02 13:57:06

标签: java swing netbeans arraylist

我已经创建了一个数组列表,我想知道如何将int添加到数组列表中。 我的程序是netbeans,所以ther是一个添加int的按钮。 我的textInput框名为studentMarksInput,添加按钮名称为addButton。

public class Adam_Ding_Student_Mark extends javax.swing.JFrame {

    static ArrayList arrayA = new ArrayList();

    public Adam_Ding_Student_Mark() {
        initComponents();
}


// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    studentMarksInput = new javax.swing.JTextField();
    addButton = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    studentMarksDisplay = new javax.swing.JTextArea();
    jScrollPane2 = new javax.swing.JScrollPane();
    studentMarksAnalysis = new javax.swing.JTextArea();
    sortButton = new javax.swing.JButton();
    analysisButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Marks Program");

    jLabel2.setText("Student Mark");

    addButton.setText("Add");
    addButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addButtonActionPerformed(evt);
        }
    });

    studentMarksDisplay.setColumns(20);
    studentMarksDisplay.setRows(5);
    jScrollPane1.setViewportView(studentMarksDisplay);

    studentMarksAnalysis.setColumns(20);
    studentMarksAnalysis.setRows(5);
    jScrollPane2.setViewportView(studentMarksAnalysis);

    sortButton.setText("jButton1");

    analysisButton.setText("jButton2");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(93, 93, 93)
            .addComponent(sortButton)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(analysisButton)
            .addGap(250, 250, 250))
        .addGroup(layout.createSequentialGroup()
            .addGap(313, 313, 313)
            .addComponent(jLabel1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(85, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addGap(52, 52, 52)
                    .addComponent(studentMarksInput, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(79, 79, 79)
                    .addComponent(addButton))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(118, 118, 118)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(212, 212, 212))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(20, 20, 20)
            .addComponent(jLabel1)
            .addGap(26, 26, 26)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(studentMarksInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(addButton))
            .addGap(13, 13, 13)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(26, 26, 26)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(37, 37, 37)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(sortButton)
                .addComponent(analysisButton))
            .addContainerGap(58, Short.MAX_VALUE))
    );

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

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


}                                         

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Adam_Ding_Student_Mark.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Adam_Ding_Student_Mark.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Adam_Ding_Student_Mark.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Adam_Ding_Student_Mark.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Adam_Ding_Student_Mark().setVisible(true);
        }
    });
}

5 个答案:

答案 0 :(得分:1)

您无法将原始int添加到列表中。只能将对象添加到List中。将原始int转换为对象,并使用以下命令将其添加到列表中:

Integer integer = Integer.valueOf(studentMarksInput.getText());
arrayA.add(integer);

答案 1 :(得分:1)

以下代码只会向您的列表中添加数值,否则只会打印一条消息:

   List<Integer> arrayA = new ArrayList<Integer>(); 

      try{   
        arrayA.add(Integer.parseInt(studentMarksInput.getText()));
         }catch (NumberFormatException e){        
         System.out.println("Error parsing input "+studentMarksInput.getText());
       }

答案 2 :(得分:0)

如果是字符串,则通过Integer类

解析整数

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

此外,您需要使用Integer类(它在对象上)而不是原语(int)

List<Integer> myList = new ArrayList<Integer>();
myList.add(Integer.parseInt("5"));
myList.add(Integer.valueOf(5));
myList.add((Integer)5);

答案 3 :(得分:0)

默认情况下,在添加集合时,会尝试将基元转换为相应的包装类。所以这里int将被转换为Integer。

答案 4 :(得分:-1)

将您的列表定义为

undefined

然后,在addButtonActionPerformed函数上添加以下行

List<Integer> arrayA = new ArrayList<Integer>()