ArrayList 有方法remove(int index)
和remove(Object o)
,所以
我尝试运行这段代码: public static void main(String args[]){
ArrayList<Long>ar=new ArrayList<Long>();
ar.add(11L);
ar.add(22L);
ar.add(33L);
ar.remove(new Integer(33)); // 1
byte b =0;
ar.remove(b); //2
for(Iterator i=ar.iterator(); i.hasNext();)
{
System.out.println(i.next());
}
}
结果我有:
22
33
我的问题:
第 1 行参数 Integer - 为什么我们没有异常?
第 2 行 - 参数字节 - 它不是 int 也不是 Object,为什么又不是 Exception?
最佳答案
remove(..)
方法不是通用的。它接受任何对象并且不检查它的类。为了删除一个元素,它应该满足的条件是equals(..)
方法在与集合中的元素进行比较时返回 true。元素 33 没有被移除,因为如果另一个对象不是long.equals(..)
则返回false
instanceof Long
byte
被解释为索引。
关于Java.数组列表。方法删除(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4382553/