java.lang.ArrayIndexOutOfBoundsException:列表中的2

java.lang.ArrayIndexOutOfBoundsException:列表中的2,java,arraylist,Java,Arraylist,我有一个方法,它必须以这种方式插入ArrayList元素: 0 Low 10 0 High 11 0 High 3 1 Low 54 为此,我对一行中的最后两个元素使用HashMap,然后将此HashMap放在ArrayList中。代码如下: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add

我有一个方法,它必须以这种方式插入ArrayList元素:

0 Low 10
0 High 11
0 High 3
1 Low 54
为此,我对一行中的最后两个元素使用HashMap,然后将此HashMap放在ArrayList中。代码如下:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    HashMap<String, String> map = new HashMap<>();
    List<HashMap<String, String>> data = new ArrayList<>();
    String label[];
    JCheckBox casella=new JCheckBox();

    //System.out.println(data.get(0).get("name"));

    if(jList2.getModel().getSize()>0){ //Se sono state selezionate PAD
        for(int i=0; i<jPanel2.getComponentCount(); i++){ //Controlla se le PAD hanno i prode
            label=(casella.getText()).split(" ");
            if( (casella=(JCheckBox) jPanel2.getComponent(i)).isSelected() ){ //Si
                map.put(label[2], label[4]);
                data.add(1,map);
            }
            else{ //No
                map.put(label[2], label[4]);
                data.add(0,map);
            }
        }
        System.out.println(data.size());

我不明白我错在哪里。

如果字符串数组是由

label=(casella.getText()).split(" ");
没有足够的元素少于2个resp。4,你会得到这个例外

在使用此数组之前,您应该先了解它的长度

线程AWT-EventQueue-0中出现异常 java.lang.ArrayIndexOutofBounds异常:2

意味着数组中没有足够的元素将其提取出来

在Java中,数组索引从0开始,所以下面的代码可能可以工作,您也可以提取map.putlab[1],label[3];由于在if和else条件下都很常见:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        HashMap<String, String> map = new HashMap<>();
        List<HashMap<String, String>> data = new ArrayList<>();
        String label[];
        JCheckBox casella=new JCheckBox();

        //System.out.println(data.get(0).get("name"));

        if(jList2.getModel().getSize()>0){ //Se sono state selezionate PAD
            for(int i=0; i<jPanel2.getComponentCount(); i++){ //Controlla se le PAD hanno i prode
                label=(casella.getText()).split(" ");
                map.put(label[1], label[3]);
                if( (casella=(JCheckBox) jPanel2.getComponent(i)).isSelected() ){ //Si 
                    data.add(1,map);
                }
                else{ //No
                    data.add(0,map);
                }
            }
            System.out.println(data.size());
}}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        HashMap<String, String> map = new HashMap<>();
        List<HashMap<String, String>> data = new ArrayList<>();
        String label[];
        JCheckBox casella=new JCheckBox();

        //System.out.println(data.get(0).get("name"));

        if(jList2.getModel().getSize()>0){ //Se sono state selezionate PAD
            for(int i=0; i<jPanel2.getComponentCount(); i++){ //Controlla se le PAD hanno i prode
                label=(casella.getText()).split(" ");
                map.put(label[1], label[3]);
                if( (casella=(JCheckBox) jPanel2.getComponent(i)).isSelected() ){ //Si 
                    data.add(1,map);
                }
                else{ //No
                    data.add(0,map);
                }
            }
            System.out.println(data.size());
}}