ArrayList.remove(int index) is working with the anonymous instance of ActionListener class :-
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() ;
}
}
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
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("") ;
} 开发者_StackOverflow中文版
}
}
}
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... :(
In Demo.java you should use:
boolean b = (list.remove(i.intValue()) != null)
instead of
boolean b = list.remove(i) ;
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.
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).
The ArrayList class has two remove
methods:
- one which takes an
Object
and attempts to remove that object from the list, and - one which takes an
int
index and removes the object in the list at that index.
You declared your variable i
as an Integer
, so it is also an Object
. Therefore you will be invoking the remove(Object)
method. Nothing happens when you attempt to remove i
since your list has Pair
s in it, not Integer
s.
What you need to do is to declare your variable i
as an int
, give it a default value of 0
(you can't assign null
to an int
), and change the line
boolean b = list.remove(i) ;
to
boolean b = (list.remove(i) != null) ;
because remove(int)
returns the object removed from the list, instead of a boolean
indicating whether it removed anything.
The problem occurs because you are calling list.remove(Object)
instead of list.remove(int)
. Since i
is an Integer
(or an Object
) and not a primitive type, the JVM thinks that you are trying to remove the object rather than the actual index; thus return false because there are no Integer
object in your list.
Simply replace the code with this one:
int i;
try{
i = Integer.parseInt(deleteIndexText.getText().trim()) ;
boolean b = (list.remove(i) != null);
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("") ;
}
Yes, Java can peform
Integer i = 2, j = new Integer(10);
i = i + j;
System.out.println(i.toString());
just fine, but int
and Integer
are not the same types :)
精彩评论