import java.util.LinkedList;
import java.util.List;public class Student { Student st1 = new Student(); public static void main(String[] args) {
Student st4 = new Student();
List ls2 = st4.zhenjia();
System.out.println(ls2); } public List zhenjia() { List ls = new LinkedList(); ls.add(st1); return ls; }}
为什么会这样呢。。?
Student st1 = new Student(); 仔细看看,自己包含自己,无限递归
public class Student {
Student st1 = new Student();
public static void main(String[] args) {
Student st4 = new Student();
List<Student> ls2 = st4.zhenjia();
System.out.println(ls2); } public List<Student> zhenjia() { List<Sutdent> ls = new ArrayList<Student>(); ls.add(st1); return ls; } }
你这样不是无限循环了嘛你new Student()的时候,你new出来的那个Student又要去new一个,再new出来的那个又要new一个,无限new下去,当然就那啥了
public class Student {
public static void main(String[] args) {
Student st4 = new Student();
List<Student> ls2 = st4.zhenjia();
System.out.println(ls2); } public List<Student> zhenjia() {
Student st1 = new Student();
List<Sutdent> ls = new ArrayList<Student>();
ls.add(st1);
return ls;
}
}