比如现在给数字3要求删除,再删除6,然后给3要求按顺序插入,不要给按顺序直接删除的答案,而是查找到后再删除import java.util.*;
class s{
public int i;
s(int s){i=s;
}
}
public class display {
public static void main(String args[]){
LinkedList <s> s1=new LinkedList<s>();
for(int i=0;i<10;i++){
s a=new s(i);
Collections.addAll(s1,a);
} for(s a:s1)
System.out.print(a.i+" ");
}
}
class s{
public int i;
s(int s){i=s;
}
}
public class display {
public static void main(String args[]){
LinkedList <s> s1=new LinkedList<s>();
for(int i=0;i<10;i++){
s a=new s(i);
Collections.addAll(s1,a);
} for(s a:s1)
System.out.print(a.i+" ");
}
}
list l = new ArrayList();
l.add("aa");
l.add("bb");
l.add("cc");
for (Iterator iter = l.iterator(); iter.hasNext();) {
String str = (String)iter.next();
System.out.println(str);
}
/*迭代器用于while循环
Iterator iter = l.iterator();
while(iter.hasNext()){
String str = (String) iter.next();
System.out.println(str);
}
*/
7楼 可以不抄书吗???
可以,建议你多看看API的文档:
public boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).此外还有四个:
removeFirst() // 删除全链表中第一个元素
removeFirstOccurrence(Object o) // 删除第一个跟 o 相同的元素(equels)
removeLast() // 删除全链表中最后一个元素
removeLastOccurrence(Object o) // 删除倒数第一个跟 o 相同的元素(equels)
ls.add("Y");
ls.add("X");
ls.add("Y");
System.out.println(ls);ls.remove("X");
System.out.println(ls);
import java.util.*;
class s{
public int i;
s(int s){i=s;
}
}
public class display {
public static void main(String args[]){
LinkedList <s> s1=new LinkedList<s>();
for(int z=0;z<10;z++){
s a=new s(z);
Collections.addAll(s1,a);
}
boolean c;
LinkedList <s> s2=new LinkedList<s>();
s s3=new s(3);
System.out.println(s3.i);
s1.remove(s3);//删除没用
for(s a:s1)
System.out.print(a.i+" "); }
}
如果要用的话 应该是remove(s3.i)
如果非得删用remove(object o)的话
你的把s3加入到链表
也就是s1.add(s3)