ArrayList.remove(INT指数)与非匿名类对象工作 [英] ArrayList.remove(int index) not working with non-anonymous class object

查看:106
本文介绍了ArrayList.remove(INT指数)与非匿名类对象工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ArrayList.remove(INT指数)正在与ActionListener的类的实例匿名: -

ArrayList.remove(int index) is working with the anonymous instance of ActionListener class :-

DeleteModule.java: -

DeleteModule.java :-

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;



class MyFrame extends JFrame{

    private ArrayList<String> list = new ArrayList<String>() ; 
    private JButton btn = new JButton("Enter index to delete : ") ;
    private JTextField fld = new JTextField() ;

    MyFrame(){
        populateList() ;

        setLayout(new GridLayout(1, 2)) ;
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
        setSize(400, 60) ;

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                list.remove( Integer.parseInt( fld.getText() ) ) ;
                JOptionPane.showConfirmDialog(null, list, "Total Elements : " + list.size(), JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE) ;
            }
        });

        add(btn) ;
        add(fld) ;

        setTitle("Total Elements : " + list.size()) ;

        setVisible(true) ;
    }

    private void populateList(){
        for(int i = 1 ; i <= 5 ; ++i){
            list.add("Key " + i) ;
        }
    }
}

public class DeleteModule {

    public static void main(String[] args) {
        new MyFrame() ;
    }
}

但是,当我与原来的模​​块整合本(以下为修改后的源减少code与DeleteFromPoolListener.class的非匿名实例),它的返回删除假的。
我真的不知道为什么它不工作。

But when I am integrating this with the original module (below is the modified reduced source code with non-anonymous instance of DeleteFromPoolListener.class), it's returning false for deletion. I really don't know why it's not working.

Demo.java

Demo.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

class MyFrame extends JFrame{

    private ArrayList<Pair> list = new ArrayList<Pair>() ;

    private JButton deleteBtn = new JButton("Delete Pair at index : ") ;
    private JTextField deleteIndexText = new JTextField() ;

    MyFrame(){
        populateList() ;
        setTitle("List size : " + list.size()) ;
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;

        JPanel lowerPanel = new JPanel() ;
        lowerPanel.setLayout(new GridLayout(2, 1)) ;

        deleteBtn.addActionListener(new DeleteFromPoolListener()) ; lowerPanel.add(deleteBtn) ;
        lowerPanel.add(deleteIndexText) ;
        add(lowerPanel) ;
        pack() ;
        setVisible(true) ;
    }

    private void populateList(){
        for(int i = 1 ; i <= 5 ; ++i){
            list.add( new Pair( "Key " + i, "Value " + i ) ) ;
        }
    }


    class DeleteFromPoolListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {

            Integer i = null ;
            try{
                i = Integer.parseInt(deleteIndexText.getText().trim()) ;
                boolean b = list.remove(i) ;

                JOptionPane.showConfirmDialog(null, list + "\n\n" + "Deleted : " + b + " from index " + i, "List size : " + list.size(), JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE) ;
                setTitle("Total Pairs : " + list.size()) ;
                deleteIndexText.setText("") ;
            }
            catch(NumberFormatException nfe){
                JOptionPane.showConfirmDialog(null, "Enter numeric value in range.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE) ;
                deleteIndexText.setText("") ;
            }       
        }
    }
} 



public class Demo {

    public static void main(String[] args) {
        new MyFrame() ;
    }
}

class Pair{

    private String key ;
    private String value ;

    Pair(String k, String v){
        key = k ;
        value = v ;
    }

    public String toString() {
        return "[" + value + "]" ;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

我真的很困惑,为什么它不工作...:(

I am really confused why it's not working... :(

推荐答案

在Demo.java你应该使用:

In Demo.java you should use:

 boolean b = (list.remove(i.intValue()) != null)

而不是

 boolean b = list.remove(i) ;

的原因是如下。 ArrayList中有2个删除功能:即作为一个参数对象一个接受作为参数的int(!不是整数)和一个

The reason is the following. ArrayList has 2 remove functions: one that takes as parameter an int (not Integer!) and one that takes as a parameter an Object.

在你的第一个例子中,你使用首先删除(如您使用转换到的intValue INT())。在第二个你试图删除的整数(其中,显然不是集合中)psented对象重新$ P $。

In your first example you used the first remove (as you converted to int using intValue()). In the second you were trying to remove the Object represented by the Integer (which, obviously wasn't in the collection).

这篇关于ArrayList.remove(INT指数)与非匿名类对象工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆