【问题标题】:Array of objects created in if statement (scope issue)在 if 语句中创建的对象数组(范围问题)
【发布时间】:2014-03-08 20:13:34
【问题描述】:

程序以以下提示开始:

  1. 创建新的学生列表
  2. 搜索学生。

问题:我创建了一个对象数组并在第一个 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");
    }
}

【问题讨论】:

    标签: java arrays scope


    【解决方案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");
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        范围由块定义,块由括号{} 分隔。如果您在 if 块内创建数组,您将无法在另一个 if 块中访问它。

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

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

        【讨论】:

        • 太棒了。我可以这样做,但 x 是在第一个 if 语句中定义的。
        • @SkilletSpecial 已根据您的要求进行编辑。您可以先声明数组,然后当您拥有x 时,您可以初始化它。请注意,如果您这样做,如果您输入第二个 if 并且该数组尚未初始化,则可以得到 null。要检查这一点,您可以使用if (oClassList != null)
        • 好的,我先声明了数组。剩下的唯一问题是第二个 if 语句。 '局部变量 oClassList 可能尚未初始化。但是如果我初始化 Student[] oClassList = null;然后我得到一个 NullPointerException。
        • 是的,这就是为什么你必须检查if (oClassList != null) { // do some operations with oClassList}
        猜你喜欢
        • 2011-09-13
        • 1970-01-01
        • 2016-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-18
        • 2022-01-14
        相关资源
        最近更新 更多