public class LinkedList {
public LinkedList() {} public LinkedList(Object item) {
if(item != null) {
current = end = start = new ListItem(item);
}
} public LinkedList(Object[] items) {
if(items != null) {
for(int i = 0; i < items.length; ++i) {
addItem(items[i]);
}
current = start;
}
} public void addItem(Object item) {
ListItem newEnd = new ListItem(item);
if(start == null) {
start = end = newEnd;
} else {
end.next = newEnd;
end = newEnd;
}
} public Object getFirst() {
current = start;
return start == null ? null : start.item;
} public Object getNext() {
if(current != null) {
current = current.next;
}
return current == null ? null : current.item;
} private ListItem start = null;
private ListItem end = null;
private ListItem current = null; private class ListItem {
public ListItem(Object item) {
this.item = item;
next = null;
} @Override
public String toString() {
return "ListItem " + item ;
} ListItem next;
Object item;
}
}
我的意思是这样的链表可不可以连接一些具有继承性质的类,比如说LinkedList类对象aList的第一个元素是a类对象ainstance,第二个对象是a类的派生类b类对象binstance,依此类推,或者后面链表元素的类型是上下波动变化的?
另外还有一种情况就是链表中的元素的类完全不同,而且也没有任何继承关系。
public LinkedList() {} public LinkedList(Object item) {
if(item != null) {
current = end = start = new ListItem(item);
}
} public LinkedList(Object[] items) {
if(items != null) {
for(int i = 0; i < items.length; ++i) {
addItem(items[i]);
}
current = start;
}
} public void addItem(Object item) {
ListItem newEnd = new ListItem(item);
if(start == null) {
start = end = newEnd;
} else {
end.next = newEnd;
end = newEnd;
}
} public Object getFirst() {
current = start;
return start == null ? null : start.item;
} public Object getNext() {
if(current != null) {
current = current.next;
}
return current == null ? null : current.item;
} private ListItem start = null;
private ListItem end = null;
private ListItem current = null; private class ListItem {
public ListItem(Object item) {
this.item = item;
next = null;
} @Override
public String toString() {
return "ListItem " + item ;
} ListItem next;
Object item;
}
}
我的意思是这样的链表可不可以连接一些具有继承性质的类,比如说LinkedList类对象aList的第一个元素是a类对象ainstance,第二个对象是a类的派生类b类对象binstance,依此类推,或者后面链表元素的类型是上下波动变化的?
另外还有一种情况就是链表中的元素的类完全不同,而且也没有任何继承关系。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货