专写bug 2018-02-17 08:30 采纳率: 100%
浏览 936
已采纳

Java中的this问题??

package test1;

/**

  • Q:为什么要在i==1时的temp前要加this才行?其他的一加就出现空指针异常
  • 顺序的排列为什么是必要的?
  • **/

public class Jushon问题 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    createLink cl = new createLink();
    cl.setLen(5);
    cl.create();
    cl.show();

}

}

//创建属性类
class child{

    int no;
    child nextchild = null;
    public child(int no){
        this.no = no;
    }


}

//创建环形链表
class createLink{
    /*
     * 特殊位:第一位、最后一位
     * 且每一位都与下一位关联
     * 关联关系由游标(temp)指定
     * */
    child firstchild = null;
    child temp = null;

    //设定长度
    int len = 0;
    public void setLen(int len){
        this.len = len;
    }

    public void create(){

        for(int i = 1 ;i <= len ;i++){

            if(i == 1){

                child ch = new child(i);
                this.firstchild = ch;
                this.temp = ch;
                temp.nextchild = ch;



            }
            else if(i == len){
                child ch = new child(i);
                temp.nextchild = ch;
                temp = ch;
                temp.nextchild = firstchild;

            }
            else{

// if(i == len){
// child ch = new child(i);
// temp.nextchild = ch;
// temp = ch;
// temp.nextchild = firstchild;
// }else{

                    child ch = new child(i);
                    temp.nextchild = ch;
                    temp = ch;

// }

            }



        }
    }

    //打印环形链表
    public void show(){
        child temp = this.firstchild;
        do{
            System.out.println(temp.no);
            temp=temp.nextchild;
        }while(temp != this.firstchild);

    }
}
  • 写回答

1条回答 默认 最新

  • WuchangI 2018-02-17 09:05
    关注

    朋友你好,我试着把你的代码按你的情况改了之后,放在我的Eclipse上运行,结果如下:
    图片说明
    图片说明

    代码运行正常。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料