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());
}
}
Run Code Online (Sandbox Code Playgroud)
结果我有:
22
33
Run Code Online (Sandbox Code Playgroud)
我的问题:
在第1行参数整数 - 为什么我们没有异常?
第2行 - 参数字节 - 它不是int而不是Object,为什么不再出现异常?
该remove(..)方法不通用.它接受任何对象,不检查其类.为了删除元素,应该注意的条件是equals(..)与集合中的元素进行比较时返回true 的方法.元素33未被移除,因为如果不是另一个对象则Long.equals(..)返回falseinstanceof Long
该byte被解释为索引.
| 归档时间: |
|
| 查看次数: |
6144 次 |
| 最近记录: |