import java.util.LinkedList;public class PersonList
{
String name=null;
int age=0;
public static LinkedList list=new LinkedList();
public PersonList(String strName)
{
this.name=strName;
} public boolean insertList(PersonList pl)
{
boolean isInsert = list.add(pl);
return isInsert;
}
/*
public boolean removeList(int position)
{
return false;
}
*/
public static String[] showList()
{
String[] ss=new String[list.size()];
for(int i=0;i<list.size();i++)
{
PersonList pp =(PersonList)list.get(i);
System.out.println(pp.name);
ss[i] = pp.name;
}
return ss;
}
public static void main(String args[])
{
PersonList pl1=new PersonList("john");
pl1.insertList(pl1);
PersonList pl2=new PersonList("tom");
pl1.insertList(pl2);
PersonList pl3=new PersonList("david");
pl1.insertList(pl3);
String[] s=PersonList.showList();
for(int i=0;i<s.length;i++)
{
System.out.println(s[i]);
} }
}
{
String name=null;
int age=0;
public static LinkedList list=new LinkedList();
public PersonList(String strName)
{
this.name=strName;
} public boolean insertList(PersonList pl)
{
boolean isInsert = list.add(pl);
return isInsert;
}
/*
public boolean removeList(int position)
{
return false;
}
*/
public static String[] showList()
{
String[] ss=new String[list.size()];
for(int i=0;i<list.size();i++)
{
PersonList pp =(PersonList)list.get(i);
System.out.println(pp.name);
ss[i] = pp.name;
}
return ss;
}
public static void main(String args[])
{
PersonList pl1=new PersonList("john");
pl1.insertList(pl1);
PersonList pl2=new PersonList("tom");
pl1.insertList(pl2);
PersonList pl3=new PersonList("david");
pl1.insertList(pl3);
String[] s=PersonList.showList();
for(int i=0;i<s.length;i++)
{
System.out.println(s[i]);
} }
}
{
String name=null;
int age=0;
public static LinkedList list=new LinkedList();
public PersonList(String strName)
{
this.name=strName;
} public boolean insertList(PersonList pl)
{
boolean isInsert = list.add(pl);
return isInsert;
}
/*
public boolean removeList(int position)
{
return false;
}
*/
public static String[] showList()
{
String[] ss=new String[list.size()];
for(int i=0;i<list.size();i++)
{
PersonList pp =(PersonList)list.get(i);
//System.out.println(pp.name);
ss[i] = pp.name;
}
return ss;
}
public void insertFirst(PersonList pl)
{
list.addFirst(pl);
}
public void insertLast(PersonList pl)
{
list.addLast(pl);
}
public static void main(String args[])
{
PersonList pl1=new PersonList("john");
pl1.insertList(pl1);
PersonList pl2=new PersonList("tom");
pl1.insertList(pl2);
PersonList pl3=new PersonList("first");
pl1.insertFirst(pl3);
PersonList pl4=new PersonList("last");
pl1.insertLast(pl4);
String[] s=PersonList.showList(); for(int i=0;i<list.size();i++)
{
System.out.println(s[i]);
} }
}
public static void main(String[] args) {
LinkedList l = new LinkedList();
for(int i=0; i<10; i++)
l.add(new Person("p"+i, 20+i));
for(int i=0; i<l.size(); i++) {
Person p = (Person)l.get(i);
System.out.println(p);
}
}
}class LinkedList {
private Node head = null;
private Node tail = null;
private int size = 0;
public LinkedList() {
}
public int size() {
return size;
}
public boolean empty() {
return size==0;
}
public void add(Object o) {
if(head==null) {
head = new Node(o, null);
tail = head;
}
else {
Node temp = new Node(o, null);
tail.setNext(temp);
tail = tail.next();
}
size++;
}
//return the object with index i in the list
public Object get(int i) {
if(i < size && i >= 0) {
Node cur = head;
for(int j=0; j<i; j++)
cur = cur.next();
return cur.get();
}
return null;
}
//remove the first occurence of o in the list
public void remove(Object o) {
//do yourself
}
}class Node {
private Object item;
private Node next;
public Node(Object i, Node n) {
item = i;
next = n;
}
public void setNext(Node n) {
next = n;
}
public Node next() {
return next;
}
public Object get() {
return item;
}
} class Person {
private String name;
private int age;
public Person(String n) {
name = n;
}
public Person(String n, int a) {
name = n;
age = a;
}
public void setAge(int n) {
age = n;
}
public String toString() {
return "name: "+name + " age:"+age;
}
}
2.用一个内部类Node(外部也可)实现第三种反而是一般教科书少提的:
用java自带的java.util.LinkedList