专写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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题