Java 在if语句中创建的对象数组(范围问题)
程序开始时会提示:Java 在if语句中创建的对象数组(范围问题),java,arrays,scope,Java,Arrays,Scope,程序开始时会提示: 创建一个新的学生列表 寻找一个学生 问题:我创建了一个对象数组,并在第一个if语句中填充它,然后尝试在第二个if语句中访问它,我知道我做不到。那么,我如何创建和填充对象数组并在以后访问它呢?有什么想法吗 if(iUserSelection == 1) { System.out.println(); System.out.println("How many students?"); x = oScan.nextInt(); System.out
if(iUserSelection == 1) {
System.out.println();
System.out.println("How many students?");
x = oScan.nextInt();
System.out.println();
// flush the buffer
oScan.nextLine();
Student[] oClassList = new Student[x];
for(int i = 0; i < x; i++) {
System.out.println("*********************");
System.out.println("Student " + (i + 1) + " of " + x);
System.out.println("*********************");
oClassList[i] = new Student("","",0,0,0,0);
System.out.print("First Name: ");
oClassList[i].setFirstName(oScan.nextLine());
System.out.print("Last Name: ");
oClassList[i].setLastName(oScan.nextLine());
System.out.print("Homework average: ");
oClassList[i].setHWAve(oScan.nextInt());
System.out.print("Quiz average: ");
oClassList[i].setQuizAve(oScan.nextInt());
System.out.print("Project average: ");
oClassList[i].setProjectAve(oScan.nextInt());
System.out.print("Test average: ");
oClassList[i].setTestAve(oScan.nextInt());
// flush the buffer
oScan.nextLine();
System.out.println();
oClassList[i].printStudent();
}
}
if(iUserSelection == 2) {
// flush the buffer
oScan.nextLine();
if(oClassList[0] != null) {
System.out.println("Student search");
System.out.print("Enter last name: ");
sSearchLastName = oScan.nextLine();
System.out.print("Enter first name: ");
sSearchFirstName = oScan.nextLine();
}
for(int y = 0; y >= oClassList.length; y++) {
if(sSearchLastName == oClassList[y].lastName) {
System.out.println("found elements");
}
else
System.out.println("Error - Student not found");
}
}
if(iUserSelection==1){
System.out.println();
System.out.println(“有多少学生?”);
x=oScan.nextInt();
System.out.println();
//刷新缓冲区
oScan.nextLine();
学生[]oClassList=新学生[x];
对于(int i=0;i=oClassList.length;y++){
if(sSearchLastName==oClassList[y].lastName){
System.out.println(“找到的元素”);
}
其他的
System.out.println(“错误-未找到学生”);
}
}
你已经知道你的答案了。这是一个范围问题,因此解决方案是在第二个if也看到的“更广”范围内定义阵列。所以,基本上在if语句之前定义它。范围是由块定义的,一个块是由括号{}
分隔的。如果在If
块中创建数组,则无法在另一个If
块中访问该数组
如何解决此问题?您可以在外部块中声明数组,以便在所有块中访问它
Student[] oClassList = null;
if (iUserSelection == 1) {
// ...
oClassList = new Student[x];
// ...
}
if (iUserSelection == 2) {
if(oClassList != null)
// ...
// ...
}
要防止在if语句退出时删除数组,请在if语句之外声明它,使其具有更大的作用域。然后,当if语句退出时,可以在if语句内部填充数组,而不会超出范围。比如说,
int[] arr;
if (true) {
arr = new int[1];
arr[0] = 5;
}
System.out.println(arr[0]);
输出:
5
arr将在退出if语句时保持其值,因为它在if语句外部声明,然后在内部实例化
您更正的代码为:
Student[] oClassList; //Declared outside of both if-statements
if(iUserSelection == 1) {
System.out.println();
System.out.println("How many students?");
x = oScan.nextInt();
System.out.println();
// flush the buffer
oScan.nextLine();
oClassList = new Student[x];
for(int i = 0; i < x; i++) {
System.out.println("*********************");
System.out.println("Student " + (i + 1) + " of " + x);
System.out.println("*********************");
oClassList[i] = new Student("","",0,0,0,0);
System.out.print("First Name: ");
oClassList[i].setFirstName(oScan.nextLine());
System.out.print("Last Name: ");
oClassList[i].setLastName(oScan.nextLine());
System.out.print("Homework average: ");
oClassList[i].setHWAve(oScan.nextInt());
System.out.print("Quiz average: ");
oClassList[i].setQuizAve(oScan.nextInt());
System.out.print("Project average: ");
oClassList[i].setProjectAve(oScan.nextInt());
System.out.print("Test average: ");
oClassList[i].setTestAve(oScan.nextInt());
// flush the buffer
oScan.nextLine();
System.out.println();
oClassList[i].printStudent();
}
}
if(iUserSelection == 2) {
// flush the buffer
oScan.nextLine();
if(oClassList[0] != null) {
System.out.println("Student search");
System.out.print("Enter last name: ");
sSearchLastName = oScan.nextLine();
System.out.print("Enter first name: ");
sSearchFirstName = oScan.nextLine();
}
for(int y = 0; y >= oClassList.length; y++) {
if(sSearchLastName == oClassList[y].lastName) {
System.out.println("found elements");
}
else
System.out.println("Error - Student not found");
}
}
Student[]课程列表//在两个if语句之外声明
如果(iUserSelection==1){
System.out.println();
System.out.println(“有多少学生?”);
x=oScan.nextInt();
System.out.println();
//刷新缓冲区
oScan.nextLine();
oClassList=新生[x];
对于(int i=0;i=oClassList.length;y++){
if(sSearchLastName==oClassList[y].lastName){
System.out.println(“找到的元素”);
}
其他的
System.out.println(“错误-未找到学生”);
}
}
太好了。我可以这样做,但x是在第一个if语句中定义的。@SkilletSpecial经过编辑以符合您的要求。您可以先声明数组,然后在使用x
时可以初始化它。请注意,如果执行此操作,如果输入第二个if
且数组尚未初始化,则可以得到null
。要检查这一点,可以使用if(oClassList!=null)
Ok我首先声明了数组。唯一剩下的问题是第二个if语句。“局部变量oClassList可能尚未初始化。”但如果我初始化Student[]oClassList=null;然后我得到一个NullPointerException。是的,这就是为什么你必须检查如果(oClassList!=null){//用oClassList做一些操作}
。