关于List接口的remove(Object o)方法的对象与下标区分
2018-03-06 20:50
525 查看
import java.util.*;
public class Test14_1 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for(int i = 1; i <= 100; i++) {
list.add(i);
}
Iterator<Integer> inte = list.iterator();
while(inte.hasNext()) {
Integer out = inte.next();
System.out.print(out + " ");
}
System.out.println();
//list.remove(list.get(10)); //*list。get(10)返回的是下标为10的对象,故remove改对象
//list.remove((Integer)10); //*remove“10”这个对象
list.remove(10); //*“10”为int型,故默认为下标,不会看成是对象,所以删除的是下标为10的对象
//网上找到的解释……
//*因为remove方法是一个被重载的方法,根据传入参数有int型(既下标索引)和Object(对象)型的两种方法,
//*首先要知道直接输入一个整数如4,在没有声明变量类型的时候程序默认是int型,即使你在编译器里选用的是Object(对象)方法,
//*程序运行时也是根据重载的机制以传入参数的类型而调用方法,因此list.remove(4)调用的是根据数组下标删除数据的方法,而非根据对象删除的方法,
//*所以我们需要这样写:list.remove((Integer)4);这时4从int型被转为Integer型,程序运行时根据对象类型调用remove(Object)方法;
Iterator<Integer> it = list.iterator();
while(it.hasNext()) {
Integer out = it.next();
System.out.print(out + " ");
}
}
}
public class Test14_1 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for(int i = 1; i <= 100; i++) {
list.add(i);
}
Iterator<Integer> inte = list.iterator();
while(inte.hasNext()) {
Integer out = inte.next();
System.out.print(out + " ");
}
System.out.println();
//list.remove(list.get(10)); //*list。get(10)返回的是下标为10的对象,故remove改对象
//list.remove((Integer)10); //*remove“10”这个对象
list.remove(10); //*“10”为int型,故默认为下标,不会看成是对象,所以删除的是下标为10的对象
//网上找到的解释……
//*因为remove方法是一个被重载的方法,根据传入参数有int型(既下标索引)和Object(对象)型的两种方法,
//*首先要知道直接输入一个整数如4,在没有声明变量类型的时候程序默认是int型,即使你在编译器里选用的是Object(对象)方法,
//*程序运行时也是根据重载的机制以传入参数的类型而调用方法,因此list.remove(4)调用的是根据数组下标删除数据的方法,而非根据对象删除的方法,
//*所以我们需要这样写:list.remove((Integer)4);这时4从int型被转为Integer型,程序运行时根据对象类型调用remove(Object)方法;
Iterator<Integer> it = list.iterator();
while(it.hasNext()) {
Integer out = it.next();
System.out.print(out + " ");
}
}
}
相关文章推荐
- 关于List,ArrayList中remove(object obj)方法不能删除对象问题
- Java中list的remove方法移除的对象与下标重合了呢?
- 关于集合List的remove方法
- 关于用jav的List对象用add方法报空指针错误
- 关于List接口中ArrayList,LinkedList和Vector的取值方法的一些性能测试
- 关于List接口的add()和set()方法
- 用Collections.sort方法对list排序有两种方法 第一种是list中的对象实现Comparable接口,如下: [java] view plain copy /** * 根据ord
- List在执行remove方法不能删除指定的对象
- mybatis的mapper接口方法参数是对象,对象中包含list情况的处理
- C#中 List类型Remove()方法不能移除对象问题
- 请使用LinkedList来模拟一个队列(先进先出的特性): 1) 拥有放入对象的方法void put(Object o) 2)取出对象的方法Object get() 3)判断队列当中是否为空的方法b
- List调用remove(object);方法无法删除!
- JAVA学习中,一个关于Object对象转换时对应方法的问题
- (个人记录)关于JavaScript jQuery对象 find方法返回值为[object object]
- Java中List对象调用remove方法报错:UnsupportedOperationException
- 关于list的remove方法感悟
- 关于java List的remove方法
- 关于list.sort()中对象的属性无法调用compareTo()方法解决方案
- 关于List的toString方法调用的是否是父类Object的,explise显示是的,但其实不是,是AbstractCollection的,他复写了Object的toString
- List接口的contains方法进行实体对象类比较