有几个细节问题,不是很明白第一段:
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; //问题:我不理解 current 存在的必要,我觉得似乎不需要这个变量,请指教
}
}
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 不能直接用 start 来代替呢??
current = current.next;
}
return current == null? null : current.item;
}
private ListItem start = null;
private ListItem end = null;
private ListItem current = null;
//下面是clss ListItem
private class ListItem{
public ListItem(Object item){
this.item = item;
next = null;
}
public String toString(){
return "ListItem " + item ;
}
ListItem next;
Object item;
}
}第二段:
public class PolyLine {
public PolyLine(double[][] coords){
Point[] points = new Point[coords.length];
for(int i = 0; i<coords.length; i++){
points[i] = new Point(coords[i][0] , coords[i][1]);
}
polyLine = new LinkedList(points);
}
public PolyLine(Point[] points){
polyLine = new LinkedList(points);
}
public void addPoint(Point Point){
polyLine.addItem(Point);
}
public void addPoint(double x, double y){
polyLine.addItem(new Point(x , y));
}
public String toString(){
StringBuffer str = new StringBuffer("PolyLine:");
Point Point = (Point)polyLine.getFirst(); //这里居然强制转换对象,可这里没有类的继承它是怎么转换的?
while(Point != null){
str.append("(" + Point + ")");
Point = (Point)polyLine.getNext(); //同样~~
}
return str.toString();
}
private LinkedList polyLine;
}
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; //问题:我不理解 current 存在的必要,我觉得似乎不需要这个变量,请指教
}
}
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 不能直接用 start 来代替呢??
current = current.next;
}
return current == null? null : current.item;
}
private ListItem start = null;
private ListItem end = null;
private ListItem current = null;
//下面是clss ListItem
private class ListItem{
public ListItem(Object item){
this.item = item;
next = null;
}
public String toString(){
return "ListItem " + item ;
}
ListItem next;
Object item;
}
}第二段:
public class PolyLine {
public PolyLine(double[][] coords){
Point[] points = new Point[coords.length];
for(int i = 0; i<coords.length; i++){
points[i] = new Point(coords[i][0] , coords[i][1]);
}
polyLine = new LinkedList(points);
}
public PolyLine(Point[] points){
polyLine = new LinkedList(points);
}
public void addPoint(Point Point){
polyLine.addItem(Point);
}
public void addPoint(double x, double y){
polyLine.addItem(new Point(x , y));
}
public String toString(){
StringBuffer str = new StringBuffer("PolyLine:");
Point Point = (Point)polyLine.getFirst(); //这里居然强制转换对象,可这里没有类的继承它是怎么转换的?
while(Point != null){
str.append("(" + Point + ")");
Point = (Point)polyLine.getNext(); //同样~~
}
return str.toString();
}
private LinkedList polyLine;
}
public Object getNext(){
if(current != null){ //为什么这里的current 不能直接用 start 来代替呢??
current = current.next;
}
return current == null? null : current.item;
}当然不能用start,理由前面跟前面一样,还有个原因,start不能被改变,start保存的是链表的头节点,头节点丢掉了,你怎么遍历链表好好学数据结构吧,这些都是最基本的
public LinkedList(Object[] items){
if(items != null){
for(int i = 0; i<items.length; i++){
addItem(items[i]);
}
current = start; //问题:我不理解 current 存在的必要,我觉得似乎不需要这个变量,请指教
}
} /*就是让指针指向第一个元素 有这个变量必然有这个变量的用处 就想选择排序一样 只是在冒泡的基础上 加了个记录当前数组下标值的变量。程序就有翻天覆地的变化*/
public Object getFirst(){
current = start; //同样这里,这句代码又能代表什么呢??
return start == null? null : start.item;
} //获取第一个元素 让指针等于开始位置
public Object getNext(){
if(current != null){ //为什么这里的current 不能直接用 start 来代替呢??
current = current.next;
} //返回最后一个元素 要是换成start 那整个程序可就乱套了
/*至于第2段为什么要强制转换, 上面两行代码你也看到了,返回值是Object,Object是所有类型的父类
转换成子类是正常的,你不转换的化就会报错的 Point xxx = Object xxx 这是不行的