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

标签 java arrays scope

程序开始时会提示:

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

最佳答案

要防止数组在 if 语句退出时被删除,请在 if 语句之外声明它,从而为其提供更广泛的范围。然后,可以在 if 语句内部填充该数组,而不会在 if 语句退出时超出范围。例如,

int[] arr;
if (true) {
    arr = new int[1];
    arr[0] = 5;
}
System.out.println(arr[0]);

输出:

5

arr 在退出 if 语句时将保持其值,因为它是在 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");
    }


}

关于java - if 语句中创建的对象数组(范围问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274480/

相关文章:

android - 如何在用户停止查看应用程序 Activity 后停止服务运行。安卓

variables - 如何使用 'if' 语句中定义的变量?

java - Java 语言规范中的 Volatile 示例返回令人惊讶的行为

java - 我怎样才能使 JBehave 与期望驱动的 API 很好地协同工作?

javascript - 查找数组中具有特定键的特定值的对象的索引

javascript - 嵌套的键值对分组到一个数组中

使用 OpenSSL key 的 Java 加密

java - 取消 AsyncTask 时出错 "Cannot make a static reference to the non-static method cancel(boolean) from the type AsyncTask<Void,Integer,Void>"

javascript - 查找对象中的值翻倍需要多少天

c# - 学习数组的 C# 问题(数组数据消失)