gpt4 book ai didi

java - 如何从另一个类中定义的类创建数组

转载 作者:行者123 更新时间:2023-12-02 03:31:18 25 4
gpt4 key购买 nike

我是一名新手 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);
}
}
}

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

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

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];

但是没有任何效果,我感觉自己现在就像是捕获了救命稻草。我感觉只要有一双正确的眼睛就能很快解决这个问题。

最佳答案

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

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

这是 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 的代码

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

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

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

希望这有帮助。

关于java - 如何从另一个类中定义的类创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com