我这种写法这为什么不能形成循环?输出循环总是1
第一种写法和第二种写法有什么不同?为什么第二种就可以形成循环?
求详解,本人新手钻研中....第一种:
public class Child {
static int len; //小孩总数
int No;
static Child firstChild; //第一个小孩的位置
static Child nextChild; //下一个小孩的位置
public Child(int no) {
this.No = no;
}
}
//链表
class Catena {
Child temp;
public void show(int len) {
Child.len = len;
}
public void catenaChild(){
for (int i = 1; i <= Child.len; i++) {
if(i==1){
Child ch = new Child(i);
Child.firstChild = ch;
temp= ch;
//ch.nextChild = ch;
}else if(i==Child.len) {
Child ch = new Child(i);
temp.nextChild = ch;
temp= ch;
temp.nextChild = Child.firstChild;
}else {
Child ch = new Child(i);
temp.nextChild = ch;
temp = ch;
}
}
}
}class pay1 {
static Child temp = Child.firstChild;
public static void pay(){
do{
System.out.println(temp.No);
temp=temp.nextChild;
}while(true);
}
}public class start {
public static void main(String[] args) {
Catena catena = new Catena();
catena.show(5);
catena.catenaChild();
pay1.pay();
}
}第二种:public class Chilld {
int NO;
Chilld nextChild;
public Chilld(int No) {
//给一个编号
this.NO = No;
}
}
//环形链表
class CycLink {
//先顶一个指向链表第一个小孩的引用
//指向第一个小孩的引用不能动
Chilld firstChild ;
Chilld temp = null;
int len=0; //表示小孩总数
//设置链表的大小
public void setLen(int len) {
this.len = len;
}
//初始化环形链表
public void createLink() {
for (int i = 1; i <=len; i++) {
//创建第一个小孩
if(i==1) {
Chilld ch = new Chilld(i);
this.firstChild = ch;
this.temp = ch;
}else {
if(i==len) {
Chilld ch = new Chilld(i);
temp.nextChild = ch;
temp=ch;
temp.nextChild = this.firstChild;
}else{
Chilld ch = new Chilld(i);
temp.nextChild=ch;
temp= ch;
}
}
}
}
public void show() {
Chilld temp = this.firstChild;
do {
System.out.println(temp.NO);
temp = temp.nextChild;
}while(true);
}
}public class Test {
public static void main(String[]args) {
//Chilld ch = new Chilld(5);
CycLink cyclink = new CycLink();
cyclink.setLen(5);
cyclink.createLink();
cyclink.show();
}
}
第一种写法和第二种写法有什么不同?为什么第二种就可以形成循环?
求详解,本人新手钻研中....第一种:
public class Child {
static int len; //小孩总数
int No;
static Child firstChild; //第一个小孩的位置
static Child nextChild; //下一个小孩的位置
public Child(int no) {
this.No = no;
}
}
//链表
class Catena {
Child temp;
public void show(int len) {
Child.len = len;
}
public void catenaChild(){
for (int i = 1; i <= Child.len; i++) {
if(i==1){
Child ch = new Child(i);
Child.firstChild = ch;
temp= ch;
//ch.nextChild = ch;
}else if(i==Child.len) {
Child ch = new Child(i);
temp.nextChild = ch;
temp= ch;
temp.nextChild = Child.firstChild;
}else {
Child ch = new Child(i);
temp.nextChild = ch;
temp = ch;
}
}
}
}class pay1 {
static Child temp = Child.firstChild;
public static void pay(){
do{
System.out.println(temp.No);
temp=temp.nextChild;
}while(true);
}
}public class start {
public static void main(String[] args) {
Catena catena = new Catena();
catena.show(5);
catena.catenaChild();
pay1.pay();
}
}第二种:public class Chilld {
int NO;
Chilld nextChild;
public Chilld(int No) {
//给一个编号
this.NO = No;
}
}
//环形链表
class CycLink {
//先顶一个指向链表第一个小孩的引用
//指向第一个小孩的引用不能动
Chilld firstChild ;
Chilld temp = null;
int len=0; //表示小孩总数
//设置链表的大小
public void setLen(int len) {
this.len = len;
}
//初始化环形链表
public void createLink() {
for (int i = 1; i <=len; i++) {
//创建第一个小孩
if(i==1) {
Chilld ch = new Chilld(i);
this.firstChild = ch;
this.temp = ch;
}else {
if(i==len) {
Chilld ch = new Chilld(i);
temp.nextChild = ch;
temp=ch;
temp.nextChild = this.firstChild;
}else{
Chilld ch = new Chilld(i);
temp.nextChild=ch;
temp= ch;
}
}
}
}
public void show() {
Chilld temp = this.firstChild;
do {
System.out.println(temp.NO);
temp = temp.nextChild;
}while(true);
}
}public class Test {
public static void main(String[]args) {
//Chilld ch = new Chilld(5);
CycLink cyclink = new CycLink();
cyclink.setLen(5);
cyclink.createLink();
cyclink.show();
}
}
public class Child {
static int len; //小孩总数
int No;
static Child firstChild; //第一个小孩的位置
static Child nextChild; //下一个小孩的位置
public Child(int no) {
this.No = no;
}
}不能用 static .
用了static 你所有的 Child 对象的firstChild,nextChild 都是指向同一个对象。
static int len; // 小孩总数
int No;
Child firstChild; // 第一个小孩的位置
Child nextChild; // 下一个小孩的位置 public Child(int no) {
this.No = no;
}
}// 链表
class Catena {
Child temp;
static Child firstNo;
public void show(int len) {
Child.len = len;
} public void catenaChild() {
for (int i = 1; i <= Child.len; i++) {
if (i == 1) {
Child ch = new Child(i);
firstNo = ch;
temp = ch;
} else if (i == Child.len) {
if(i!=1){
Child ch = new Child(i);
ch.firstChild = temp;
ch.nextChild = firstNo;
temp.nextChild=ch;
temp = ch;
}else{
temp.firstChild=temp;
temp.nextChild=temp;
}
} else {
Child ch = new Child(i);
ch.firstChild = temp;
temp.nextChild = ch;
temp = ch;
}
}
}
}class pay1 {
static Child temp = Catena.firstNo; public static void pay() {
do {
System.out.println(temp.No);
temp = temp.nextChild;
} while (true);//temp.No!=Catena.firstNo.No
}}
static int len; // 小孩总数
int No;
Child firstChild; // 第一个小孩的位置
Child nextChild; // 下一个小孩的位置 public Child(int no) {
this.No = no;
}
}// 链表
class Catena {
Child temp;
static Child firstNo;
public void show(int len) {
Child.len = len;
} public void catenaChild() {
for (int i = 1; i <= Child.len; i++) {
if (i == 1) {
Child ch = new Child(i);
firstNo = ch;
temp = ch;
if(Child.len==1){
temp.firstChild=temp;
temp.nextChild=temp;
}
} else if (i == Child.len) {
Child ch = new Child(i);
ch.firstChild = temp;
ch.nextChild = firstNo;
temp.nextChild=ch;
temp = ch;
} else {
Child ch = new Child(i);
ch.firstChild = temp;
temp.nextChild = ch;
temp = ch;
}
}
}
}class pay1 {
static Child temp = Catena.firstNo; public static void pay() {
do {
System.out.println(temp.No);
temp = temp.nextChild;
} while (true);//temp.No!=Catena.firstNo.No
}}
另外你的第二个例子,也有点问题,cyclink.setLen(5);时,会出错,
你可以修改为:// 创建第一个小孩
if (i == 1) {
Chilld ch = new Chilld(i);
this.firstChild = ch;
this.temp = ch;
if(len==1){
temp.nextChild=temp;
}
} else {
...
}