在if语句中创建的对象数组(范围问题)

时间:2014-03-08 20:13:35

标签: java arrays scope

程序首先提示:

  1. 创建新的学生列表
  2. 搜寻学生。
  3. 问题:我创建了一个对象数组并在第一个if语句中填充它然后尝试在第二个if语句中访问它,我知道我不能这样做。那么我如何创建和填充对象数组并在以后访问它?有什么想法吗?

    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");
        }
    }
    

3 个答案:

答案 0 :(得分:1)

要在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");
    }


}

答案 1 :(得分:0)

你已经知道答案了。这是一个范围问题,因此解决方案是将您的数组定义在“更宽”的范围内,您的第二个也可以看到。所以,基本上在if语句之前定义它。

答案 2 :(得分:0)

范围由块定义,块由括号{}分隔。如果您在if块内创建数组,则无法在其他if块中访问该数组。

如何解决这个问题?您可以在外部块中声明数组,以便可以在所有块中访问它。

Student[] oClassList = null;

if (iUserSelection == 1) {
    // ...
    oClassList = new Student[x];
    // ...
}

if (iUserSelection == 2) {
    if(oClassList != null)
        // ...
    // ...
}