建構
LinkedList L = new LinkedList();
LinkedList<E> L = new LinkedList();
Example1 一般常用
LinkedList<String> L = new LinkedList();
L.add("d"); //新增元素
L.add("c");
L.add("a");
L.add("b");
L.remove(0); //remove() 刪除第0個元素 會遞補
Collections.sort(L); //排列String or Integer
for(String s:L)
{
System.out.println(s);
}
System.out.println(L.get(1)); //get() 取資料
Example2 元件的sort
import java.util.*;
public class test
{
class NameCompare implements Comparator<A>
{
public int compare(A a1,A A2)
{
return a1.GetName().compareTo(A2.GetName());
}
}
public static void main(String[] args)
{
new test().Go();
}
void Go()
{
LinkedList<A> LA = new LinkedList();
A Tony = new A("Tony");
A John = new A("John");
A Andy = new A("Andy");
LA.add(Tony);
LA.add(John);
LA.add(Andy);
NameCompare NC = new NameCompare();
Collections.sort(LA, NC);
for(A An:LA)
{
System.out.println(An.Name);
}
}
}
class A
{
String Name;
A(String Name)
{
this.Name = Name;
}
public String GetName()
{
return Name;
}
}