让我们逐步介绍您的代码:Engine engine = new Engine();创建新的engine对象,将所有类变量实例化为其默认值.原始int的默认值为0;在初始化时,您已经:public int numDigits; // 0public int[] secretNumber = new int[numDigits]; // arrays object of size 0public Random randomNumberGenerator; // null如何进行?问题部分出在对象设计中-您需要确定约束状态变量的不变量.您需要在numDigits初始化期间设置数组的大小:public int[] secretNumber; // null at the moment of object initializationpublic void setNumDigits() { Scanner setNumDigits = new Scanner(System.in); System.out.println("Enter the number of digits to use"); numDigits = Integer.parseInt(setNumDigits.nextLine()); secretNumber = new int[numDigits];}I'm creating an int array secretNumber. When I declare the array size as a number, there's no out of bounds exception, but when I declare the array size with a variable (numDigits) I get the out of bounds exception at Index 0 for the line ' secretNumber[i] = val '. This is the class:import java.util.Random;import java.util.Scanner;public class Engine { public int numDigits; public int[] secretNumber = new int[numDigits]; //this is the array public Random randomNumberGenerator; public void setNumDigits() { Scanner setNumDigits = new Scanner(System.in); System.out.println("Enter the number of digits to use"); String numDigits = setNumDigits.nextLine(); this.numDigits = Integer.parseInt(numDigits); } public int getNumDigits() { return this.numDigits; } public void generateNewSecret() { int val; for (int i = 0; i < numDigits - 1; i++) { Random rand = new Random(); val = rand.nextInt(9); secretNumber[i] = val; //out of bounds exception is here } } public void setSecretNumber(int[] secretNumberCopy) { secretNumberCopy.equals(this.secretNumber); } public int[] getSecretNumber() { return secretNumber;}}This is the main that executes the methods, and I run the numDigits setter before setting the array: import java.util.Scanner;public class Bagels { public static void main(String[] args) { Player playerOne = new Player(); playerOne.setName(); System.out.println(playerOne.getName()); Engine engine = new Engine(); engine.setNumDigits(); engine.setSecretNumber(engine.secretNumber); engine.generateNewSecret(); System.out.println(engine.getSecretNumber()); }}Why would Index 0 be out of bounds if I've set numDigits?? 解决方案 While we have already some clarification to the reason you got the exception there is still a question how to fix it and how to avoid such cases in future.Let's go through your code step-by-step:Engine engine = new Engine();New engine object is created an all class variables instantiated to their default values. Default value for primitive int is 0;At the moment of initialization you have:public int numDigits; // 0public int[] secretNumber = new int[numDigits]; // arrays object of size 0public Random randomNumberGenerator; // nullHow to proceed with that?The issue is partially in object design - you need to identify the invariants that constrain the state variables. You need to set the size of the array during numDigits initialization:public int[] secretNumber; // null at the moment of object initializationpublic void setNumDigits() { Scanner setNumDigits = new Scanner(System.in); System.out.println("Enter the number of digits to use"); numDigits = Integer.parseInt(setNumDigits.nextLine()); secretNumber = new int[numDigits];} 这篇关于用变量声明数组大小会导致超出范围的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!