如何从另一个 class 中定义的 class 创建一个数组

How to make an array from a class defined inside another class

我是一个新手 Java 程序员,试图使用在不同文件中定义的 类。所以,我写了这两个 .java 文件:

首先是MyLibrary.java:

package mymainprogram;

public class MyLibrary {
    public class MyRecord {
        int number;
        char letter;
    }

    public static int TriplePlusThree(int input_number) {            
        return ((input_number*3) + 3);
    }
}

然后,MyMainProgram.java:

package mymainprogram;

import java.util.Scanner;

public class MyMainProgram {
    public static void main(String[] args) {
        Scanner keyread = new Scanner(System.in);

        System.out.print("Enter Number to Process: ");
        int num = keyread.nextInt();  
        int result = MyLibrary.TriplePlusThree(num);
        System.out.println("3x + 3 = "+result);

        String letters = "ABCDEFGHIJ";
        MyLibrary.MyRecord[] TenRecs = new MyLibrary.MyRecord[10];

        for (int i = 0; i < 10; i++) {
            TenRecs[i].number = i;      //NullPointerException here
            TenRecs[i].letter = letters.charAt(i);           
        }
    }
}

我没问题让这个方法正常工作;现在我的目标是创建一个数组,其中数组的每个成员都有一个整数和一个字符。 (注意:我并不是在寻找更好的方法来完成这个 objective;我只是在使用这个简单的例子来尝试让它工作)。

当我尝试 运行 我的程序时,我得到:

java.lang.NullPointerException

我研究了这个,发现 this page,上面写着:

If we try to access the objects even before creating them, run time errors would occur. For instance, the following statement throws a NullPointerException during runtime which indicates that [this array] isn't yet pointing to [an] object. The objects have to be instantiated using the constructor of the class and their references should be assigned to the array elements in the following way.

studentArray[0] = new Student();

所以,我尝试在我的主程序中这样做:

MyRecordArray[0] = new MyLibrary.MyRecord();

但这会导致此错误:

an enclosing instance that contains MyLibrary.MyRecord is required

那个错误消息让我看到 this Stack Exchange question,它说:

you have to create an object of X class (outer class) and then use objX.new InnerClass() syntax to create an object of Y class.

X x   = new X();
X.Y y = x.new Y();

因此,根据该答案,我将这两行添加到我的程序中:

MyLibrary mylibrary         = new MyLibrary();
MyLibrary.MyRecord myrecord = mylibrary.new MyRecord();

这些行没有给出任何警告或编译错误,所以我觉得我更近了一步,但我仍在努力弄清楚如何制作数组。我知道如果我想制作一个整数数组,我会简单地这样做:

int[] TenInts = new int[10];

所以,我尝试过类似的方法:

myrecord[] TenRecs = new myrecord[10];
MyRecord[] TenRecs = new MyRecord[10];

但是没有任何效果,我觉得我现在正在抓住救命稻草。我觉得合适的眼睛可以很快解决这个问题。

有几点需要注意。 首先,实例内部 class 和静态内部 class.

之间的区别

实例内部 class,声明时没有 static 修饰符,

public class OutterClass {
    public class InstanceInnerClass {}
}

应该这样创建:

OutterClass outter = new OutterClass();
InstanceInnerClass iInner = outter.new InstanceInnerClass();

虽然静态内部 class,使用 static 修饰符声明,

public class OutterClass {
    public static class StaticInnerClass {}
}

应该这样创建:

StaticInnerClass sInner = new OutterClass.StaticInnerClass();

其次,您在数组条目被填充之前访问了它

MyLibrary library = new MyLibrary();
MyLibrary.MyRecord[] TenRecs = new MyLibrary.MyRecord[10];
for (int i = 0; i < 10; i++) {
    // Create new instance
    TenRecs[i] = library.new MyRecord();
    TenRecs[i].number = i;
    TenRecs[i].letter = letters.charAt(i);
}

如果初始化 MyRecord[10] 数组有空对象。您仍然必须将数组中的每个元素初始化为一个新的 MyRecord 对象。否则你会得到 NPE.

一种方法是:List<MyRecord> TenRecs = new ArrayList<MyRecord>(); TenRecs.add( new MyRecord() );

for ( int i = 0; i < 10; i++ ) TenRecs[i] = new MyRecord();

另外,如果您添加导入语句:import mymainpackage.MyLibrary.MyRecord; 您不需要做 mylibrary.new MyRecord(); 只需做 new MyRecord();

您需要将内部 class 声明为静态。

您可以按如下方式修改代码以满足您的要求:

这是 MyLibrary 的代码

public class MyLibrary {

    public static class MyRecord{
        int number;
        char letter;

        public MyRecord(){
            number = 0;
            letter = '[=10=]';
        }

        public MyRecord(int number, char letter){
            this.number = number;
            this.letter = letter;
        }
    }

    public static int TriplePlusThree(int input_number){ 
        return (input_number * 3 + 3);
    }
}

这是 MyMainProgram 的代码

import java.util.Scanner;

public class MyMainProgram {

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number to process");
        int num = in.nextInt();
        System.out.println("3x + 3 = " + MyLibrary.TriplePlusThree(num));

        String letters = "ABCDEFGHIJ";
        MyLibrary.MyRecord[] TenRecords = new MyLibrary.MyRecord[2];

        for (int i=0; i<TenRecords.length; i++){
            TenRecords[i] = new MyLibrary.MyRecord();
            TenRecords[i].number = i;
            TenRecords[i].letter = letters.charAt(i); 
        }

        // Printing class records
        for (int i=0; i<TenRecords.length; i++){
            System.out.println("Printing records of record " + i + " : ");
            System.out.println("Number : " + TenRecords[i].number);
            System.out.println("Letter : " + TenRecords[i].letter);
        }
        in.close();
    }
}

您可以按如下方式创建内部 class 的实例:

TenRecords[i] = new MyLibrary.MyRecord();

希望这对您有所帮助。

您必须在初始化之前在数组中创建每个对象。参考this link.

像这样创建每个对象。

MyLibrary outer = new MyLibrary();
TenRecs[i] = outer.new MyRecord();

完整代码:

MyLibrary.MyRecord[] TenRecs = new MyLibrary.MyRecord[10];

    for (int i = 0; i < 10; i++) {
        MyLibrary outer = new MyLibrary();
        TenRecs[i] = outer.new MyRecord();
        TenRecs[i].number = i; 
        TenRecs[i].letter = letters.charAt(i);  
    }

嵌套的 class MyRecord 包含对外部 class MyLibrary 的隐藏引用,因此必须与 MyLibrary 的实例相关联。这样 MyRecord 可以访问 MyLibrary.

的私有成员
MyLibrary.MyRecord myrecord = mylibrary.new MyRecord();

哇,这语法很有趣。在我多年的 java 编程生涯中,我从未使用过这样的结构。通常,您会在外部 class (MyLibrary) 中创建内部 classes (MyRecord) 的对象。另一个常见的事情是将内部 class 声明为 static 这将消除对外部 class.

实例的需要
MyRecord[] TenRecs = new MyRecord[10];

这将创建一个所有元素都为 NULL 的数组。您必须初始化它们中的每一个(例如,使用循环)。