Exception in thread ‘main ‘ java.lang.ArrayIndexOutOfBoundsException: 1
场景:1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Lottal.getRandom(Lottal.java:21) at Lottal.main(Lottal.java:34) ------------------------------------------------ import java.lang.reflect.Array; import java.util.Random; public class Lottal { private int num; private int length; private int[] array=new int[length];//length是在构造方法之后才能赋值 public Lottal(int num, int length) { this.num = num; this.length = length; } public void getRandom(){ Random rnd = new Random(); for(int i=0;i<length;i++){ System.out.print(" "+rnd.nextInt(num+1)); array[i]=rnd.nextInt(num+1); } } public static void main(String[] args) { Lottal lottal = new Lottal(3,3); lottal.getRandom(); } } ---------------------- 改完之后 import java.lang.reflect.Array; import java.util.Random; public class Lottal { private int num; private int length; private int[] array; public Lottal(int num, int length) { this.num = num; this.length = length; } public void getRandom(){ Random rnd = new Random(); array=new int[length]; for(int i=0;i<length;i++){ System.out.print(" "+rnd.nextInt(num+1)); array[i]=rnd.nextInt(num+1); } } public static void main(String[] args) { Lottal lottal = new Lottal(93,3); lottal.getRandom(); } }