Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList删除方法多态性_Java_Inheritance_Arraylist_Polymorphism - Fatal编程技术网

Java ArrayList删除方法多态性

Java ArrayList删除方法多态性,java,inheritance,arraylist,polymorphism,Java,Inheritance,Arraylist,Polymorphism,我使用的是ArrayList,我注意到有两种删除方法: 从列表界面中继承的一个: public boolean remove(Object o) 一个在ArrayList中实现: public Object remove(int index) 在我的情况下,我将调用list.remove(2),将调用哪个方法?为什么?因为我的“对象”也是一个整数 谢谢 public Object remove(int index) 将被调用 由于您正在调用list.remove(2),因此参数的类型为in

我使用的是
ArrayList
,我注意到有两种删除方法:

列表
界面中继承的一个:

public boolean remove(Object o)
一个在
ArrayList
中实现:

public Object remove(int index)
在我的情况下,我将调用
list.remove(2),将调用哪个方法?为什么?因为我的“对象”也是一个整数

谢谢

public Object remove(int index)
将被调用


由于您正在调用
list.remove(2)
,因此参数的类型为int。因此,最具体的匹配值的方法将获得调用,在这种情况下,如果您这样调用该方法,则
remove(int index)
index

处的值将被删除:

intList.remove(2);
intList.remove(new Integer(2)));
第二项将被删除。 如果您这样调用该方法:

intList.remove(2);
intList.remove(new Integer(2)));

将从ArrayList上的JavaDocs中删除对象2。

。删除(对象obj):

从此列表中删除指定元素的第一个匹配项, 如果有。如果列表不包含该元素,则为 不变。更正式地说,删除索引i最低的元素 使得(o==null?get(i)==null:o.equals(get(i))(如果 元素存在)。如果此列表包含指定的 元素(或等效,如果此列表因 电话)

因为它使用o.equals(),所以它要求包含的元素A)是对象,B)等于remove()参数

下面是一个示例来演示

/**
 * 
 */
package ksf;

import java.awt.List;
import java.util.ArrayList;

/**
 * @author Kelly French
 *
 */
public class ListRemove {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList l = new ArrayList();

        l.add(new Integer(0)); // to take up the zeroth spot
        l.add(new Integer(300)); // 1st added, position [1]
        l.add(new Integer(100)); // 2nd added, position [2]
        l.add(new Integer(200)); // 3rd added, position [3]

        // will remove whatever is at location 2, i.e. Integer(100)
        l.remove(2); 

        // will remove first object Integer(200) if it exists
        //  in this case it is the equivalent of l.remove(3);
        l.remove(new Integer(200));

        // will cause nothing to be removed
        l.remove(200); // no 200th element in the list
        l.remove(new Integer(2)); // no element passes o.equals()
    }
}

这是一个有点假的问题,因为一个简单的测试大约在一分钟内就能回答这个问题。托马斯抢先回答了我——如果不确定像这样简单的问题,试试看。写一个简单的程序,看看结果如何。你花在StackOverflow上的时间大致与张贴到StackOverflow上的时间相同。Charles Goodwin比我快:-)@Thomas Carpenter关于调用哪个函数的问题可以在一分钟内回答,但关于为什么仍然需要回答的问题。@bstick12:在这种情况下,编译器会进行最具体的调用,这确实是对int-index的调用。请看我的答案。我想补充一点,这种不一致性是java collections api重新设计的结果。好的,如果我需要调用另一个方法,我能以某种方式做到吗?也许可以将其向上转换为List,这样另一个方法将是“最具体”的方法?@Boris C:或者您可以传递
Integer.valueOf(2)
@Boris C-No,该方法的另一种形式是,当您有一个对象的实例要从列表中删除,但不知道该对象在列表中的位置时。@Boris C-很公平,但如果您有一个整数(2),那么使用remove(2)仍然不起作用,因为编译器会将“2”视为int,并始终删除该索引处的元素。只要目标对象的equals()方法行为正常,您的建议就会起作用。