首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求解析

2012-04-16 
求大虾解析public class TestCount{public static void main(String[] args){kidCicrle kc new kidCicrl

求大虾解析
public class TestCount{
public static void main(String[] args){
kidCicrle kc = new kidCicrle(0);
int countNum = 0;
kid k = kc.first;
while(kc.count >1){
countNum++;
if(countNum == 3){
countNum = 0;
kc.delete(k);
}
k = k.right;
}
System.out.println(kc.first.id);

}
}

class kid{
int id;
kid right;
kid left;

}

class kidCicrle{
int count = 0;
kid first,last;

kidCicrle(int n){
for(int i = 0;i<n;i++){
add();
}

}


void add(){
kid k = new kid();
k.id = count;
 if(count<=0){
first = k;
last = k;
k.right = k;
k.left = k;
}
else{
last.right = k;
k.right = first;
last = k;
}
count++;
}

void delete(kid k){
if(count <= 0){
System.out.println("error");
}else if(count == 1){
first = last = null;
}
else{
k.left.right = k.right;
k.right.left = k.left;
if(k == first){
first = k.right;
}
if(k == last){
last = k.left;
}
}
count--;
}
}





程序编译能通过,但运行时却抛出异常
Exceprion in thread "main" java.lang.NullpointerException
  at kidCicrle.delete<TestCount.java:62>
  at TesrCount.main<TestCount.java:10>

[解决办法]
Exceprion in thread "main" java.lang.NullpointerException
at kidCicrle.delete<TestCount.java:62>

这两句话,你没考虑有可能 k.left为null 以及 k.right为null 的特殊情况:
k.left.right = k.right;
k.right.left = k.left;
[解决办法]

Java code
public class TestCount {    public static void main(String[] args) {        kidCicrle kc = new kidCicrle(20);        int countNum = 0;        kid k = kc.first;        while (kc.count>0&&k!=null){//加k!=null约束            countNum++;            if (countNum == 3){                countNum = 0;                kc.delete(k);            }            k = k.son;        }        for(kid k1 = kc.first;k1 != null;k1 = k1.son){//链表遍历            System.out.print(k1.id +" ");        }    }}class kid {    int id = 0;    kid father = null;    kid son = null;}class kidCicrle {    int count = 0;    kid first = null;    kid last = null;    kidCicrle(int n) {        for (int i = 0;i<n;i++){//你赋值为0,对照你自己的程序            add();        }    }        void add() {        kid k = new kid();        k.id = count;        if (count==0){            first=k;            last=k;            first.son = last;            last.father = first;        }        else{            k.father = last;//注意双向都要连,避免照成空指针异常            last.son = k;            last = k;        }        count++;    }    void delete(kid k){        if (count <= 0){            System.out.println("error");        }else if (count==1){            first = null;            last = null;        }else {//双向链表,把引用搞清楚。            if (k==first){                k.son.father = null;                first = k.son;            }            else if (k==last){                k.father.son=null;                last = k.father;            }            else{                k.son.father = k.father;                k.father.son = k.son;            }        }        count--;    }} 

热点排行