ArrayList.remove(int index) 不适用于非匿名类对象
ArrayList.remove(int index) 正在使用 ActionListener 类的匿名实例:
-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() ;
}
}
但是当我将其与原始模块集成时(下面是带有非匿名实例的 DeleteFromPoolListener.class 的修改后的简化源代码) ),删除时返回 false。 我真的不知道为什么它不起作用。
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;
}
}
我真的很困惑为什么它不起作用......:(
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("") ;
}
}
}
}
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... :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Demo.java 中,您应该使用:
而不是
原因如下。 ArrayList 有 2 个删除函数:一个以 int(不是 Integer!)作为参数,另一个以 Object 作为参数。
在第一个示例中,您使用了第一个删除(当您使用 intValue() 转换为 int 时)。在第二次中,您试图删除由整数表示的对象(显然它不在集合中)。
In Demo.java you should use:
instead of
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).
ArrayList 类有两个
remove
方法:Object
并尝试从列表中删除该对象,另int
索引并删除列表中的对象列出该索引。您将变量
i
声明为Integer
,因此它也是一个Object
。因此,您将调用remove(Object)
方法。当您尝试删除i
时,不会发生任何事情,因为您的列表中有Pair
,而不是Integer
。您需要做的是将变量
i
声明为int
,并为其指定默认值0
(您不能分配 < code>null 为int
),并将该行更改为
因为
remove(int)
返回从列表中删除的对象,而不是布尔值
指示是否删除了任何内容。The ArrayList class has two
remove
methods:Object
and attempts to remove that object from the list, andint
index and removes the object in the list at that index.You declared your variable
i
as anInteger
, so it is also anObject
. Therefore you will be invoking theremove(Object)
method. Nothing happens when you attempt to removei
since your list hasPair
s in it, notInteger
s.What you need to do is to declare your variable
i
as anint
, give it a default value of0
(you can't assignnull
to anint
), and change the lineto
because
remove(int)
returns the object removed from the list, instead of aboolean
indicating whether it removed anything.出现此问题的原因是您调用的是
list.remove(Object)
而不是list.remove(int)
。由于i
是一个Integer
(或一个Object
)而不是原始类型,JVM认为您正在尝试删除对象而不是实际索引;因此返回 false,因为列表中没有 Integer 对象。只需将代码替换为以下代码:
是的,Java 可以很好地执行
,但
int
和Integer
不是相同的类型:)The problem occurs because you are calling
list.remove(Object)
instead oflist.remove(int)
. Sincei
is anInteger
(or anObject
) 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 noInteger
object in your list.Simply replace the code with this one:
Yes, Java can peform
just fine, but
int
andInteger
are not the same types :)