我的list.remove(index) 不能实现。
程序如下,Name是我自己创建的类,,想删除list中Name那一项,list.remove(3),实现不了。。importjava.util.*;publicclass...
程序如下,Name是我自己 创建的类,,想删除list中Name那一项,list.remove(3),实现不了。。
import java.util.*;
public class ListTest{
public static void main(String[] args){
Collection list = new LinkedList();
list.add(new String("qwe"));
list.add("qwe");
list.add(new Integer(100));
list.add(new Name("Li","lei"));
list.remove(3); //这一项不能实现。
Name f4 = new Name("Li","lei");
System.out.println(list.size());
System.out.println(list);
Name f3 = new Name("Li","lei");
System.out.println(f4.equals(f3));
}
}
class Name{
private String firstName,lastName;
public Name(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String toString(){
return "Name:"+firstName+"\t"+lastName;
}
public boolean equals(Object oo){
if(oo instanceof Name){
Name nam = (Name)oo;
return (firstName.equals(nam.firstName))
&& lastName.equals(nam.lastName);
}
return super.equals(oo);
}
public int hashCode(){
return firstName.hashCode();
}
} 展开
import java.util.*;
public class ListTest{
public static void main(String[] args){
Collection list = new LinkedList();
list.add(new String("qwe"));
list.add("qwe");
list.add(new Integer(100));
list.add(new Name("Li","lei"));
list.remove(3); //这一项不能实现。
Name f4 = new Name("Li","lei");
System.out.println(list.size());
System.out.println(list);
Name f3 = new Name("Li","lei");
System.out.println(f4.equals(f3));
}
}
class Name{
private String firstName,lastName;
public Name(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String toString(){
return "Name:"+firstName+"\t"+lastName;
}
public boolean equals(Object oo){
if(oo instanceof Name){
Name nam = (Name)oo;
return (firstName.equals(nam.firstName))
&& lastName.equals(nam.lastName);
}
return super.equals(oo);
}
public int hashCode(){
return firstName.hashCode();
}
} 展开
1个回答
2014-08-16
展开全部
public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractSequentialList<E>
Parameters:
index - the index of the element to be removed
Returns:
the element previously at the specified position
注意函数返回的是索引位置之前的元素
你分别
list.remove(3);
list.remove(2);
list.remove(1);
看看结果就知道了
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractSequentialList<E>
Parameters:
index - the index of the element to be removed
Returns:
the element previously at the specified position
注意函数返回的是索引位置之前的元素
你分别
list.remove(3);
list.remove(2);
list.remove(1);
看看结果就知道了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询