如何从另一个类中定义的类创建数组
我是Java的新手程序员,正在尝试使用在其他文件中定义的类.因此,我编写了以下两个.java文件:
I am a novice Java programmer trying to use classes defined in a different file. So, I've written these two .java files:
首先,有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);
}
}
}
我完全可以使方法正常工作.现在我的目标是创建一个数组,其中数组的每个成员都有一个整数和字符.(注意:我不是在寻找实现此目标的更好方法;我只是在使用这个简单的示例来尝试实现此目的).
I had no problem getting the method to work just fine; now my goal is to create an array where each member of the array has an integer and character. (Note: I'm not looking for better ways to accomplish this objective; I'm merely using this trivial example to try to get this working).
当我尝试运行程序时,我得到了:
When I tried to run my program, I got:
java.lang.NullPointerException
java.lang.NullPointerException
我对此进行了研究,并发现了此页面,其中说:
I researched this, and found this page, which says:
如果即使在创建对象之前尝试访问对象,也会发生运行时错误.例如,以下语句在运行时抛出NullPointerException,它指示[this array]尚未指向[an]对象.必须使用该类的构造函数实例化这些对象,并应通过以下方式将它们的引用分配给数组元素.
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();
因此,我尝试在主程序中做到这一点:
So, I tried to do that in my Main Program:
MyRecordArray[0] = new MyLibrary.MyRecord();
但是会出现此错误:
包含MyLibrary.MyRecord的封闭实例是必需的
an enclosing instance that contains MyLibrary.MyRecord is required
该错误消息导致我此Stack Exchange问题,内容为:
That error message led me to this Stack Exchange question, which says:
您必须创建X类(外部类)的对象,然后使用objX.new InnerClass()语法创建Y类的对象.
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();
因此,根据该答案,我在程序中添加了以下两行:
So, in accordance with that answer, I've added these two lines to my program:
MyLibrary mylibrary = new MyLibrary();
MyLibrary.MyRecord myrecord = mylibrary.new MyRecord();
这些行不给出任何警告或编译错误,所以我觉得我离它更近了一步,但我仍在尝试弄清楚如何制作一个数组.我知道如果我想创建一个整数数组,我会简单地做到这一点:
Those lines don't give any warnings or compilation errors, so I feel like I'm one step closer, but I'm still trying to figure out how to make an array. I know if I wanted to make an array of integers, I would simply do this:
int[] TenInts = new int[10];
所以,我尝试过类似的事情:
So, I've tried things like:
myrecord[] TenRecs = new myrecord[10];
MyRecord[] TenRecs = new MyRecord[10];
但是没有任何效果,我感觉我现在正在抓稻草.我觉得合适的眼睛可以很快解决这个问题.
But nothing is working, and I feel like I'm grasping at straws now. I get the feeling that the right set of eyes could solve this pretty quickly.
您需要将内部类声明为静态.
You need to declare the inner class as static.
您可以按照以下要求修改代码:
You can modify the code as follows to suit your requirements:
这是MyLibrary的代码
This is the code for MyLibrary
public class MyLibrary {
public static class MyRecord{
int number;
char letter;
public MyRecord(){
number = 0;
letter = '\0';
}
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的代码
This is the code for the 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();
}
}
您可以如下创建内部类的实例:
You can create the instance of the inner class as follows:
TenRecords[i] = new MyLibrary.MyRecord();
希望这会有所帮助.