0

私はJavaが初めてです。これはエラーを出しています。

private class applicantInfo {
    int Id;
    double quality;
}
private class allApplicants {
    applicantInfo[] applicantArr = new applicantInfo[20];
}
public void newGame {
    allApplicants applicants = new allApplicants();
    applicants.applicantArr[0].Id = 5;
}

の時点でエラーが発生していますapplicants.applicantArr[0].Id = 5;

私がやりたいことは、Cでこれに似ています:

typedef struct _applicantInfo{
    int Id;
    double quality;
} applicantInfo;

typedef struct _allApplicants {
    applicantInfo applicantArr[20];
} allApplicants;

int main () {
    allApplicants applicants;
    applicants.applicantArr[0].Id = 5;  
}

Javaでそれを行うにはどうすればよいですか?

4

3 に答える 3

4

Java 配列と C 配列の違いは、C は配列内のすべての値を初期化するのに対し、Java はそれらを null に設定することです。だからあなたが電話するとき

applicants.applicantArr[0].Id = 5;

applications.applicantArr[0] が null であるため、NullPointerException が発生します。アクセスする前に、新しいapplicantInfoを作成して配列に入れる必要があります。

allApplicants applicants = new allApplicants();
applicants.applicantArr[0] = new applicantInfo();
applicants.applicantArr[0].Id = 5;
于 2013-03-18T05:28:10.693 に答える
1

newGame() でこれを行う必要があります。

applicantInfo item = new applicantInfo();//first create a applicantInfo object
item.Id= 5;//set the object properties
applicants.applicantArr[0]= item;//assign the object to the array

これは、 では配列が とは異なる方法で機能するためJavaですCこれを見てください

また、ここにあなたが始めるためのチュートリアルがあります.

于 2013-03-18T05:26:28.800 に答える
0

コメントと TODO を含むコードの高レベル構造を提案します。詳細を入力できます。メソッドの構造を提案する最後の部分newGameは、発生しているエラーを取り除くのに役立ちます。

ApplicantInfoクラスの構造:

public class ApplicantInfo {
    private int ID;
    private double quality;

    // Constructor to create an instance with the specified ID value
    public ApplicantInfo(int id){
        // TODO: Initialize the value for ID field
    }

    // Method to get the value for ID
    public int getID(){
        // TODO: return value of ID field
    }

    // Method to set the value for ID
    public void setID(int id){
        // TODO: set the value for ID field
    }

    // Getter and setter methods for "quality" 
    // on the lines of the above methods
}

AllApplicantsクラスの構造:

public class AllApplicants {
    private ApplicantInfo[] applicantArr = new ApplicantInfo[20];

    // Method to get the applicant info at a given index
    public ApplicantInfo getApplicant(int index){
        // TODO: Get the applicant from the array present at the specified index
    }

    // Method to add an applicant info at a given index
    public boolean addApplicant(ApplicantInfo applicant, int index){
        // TODO: Try to add the specified applicant to the array at the specified index
        // Return true to indicate that the applicant was successfully added, 
        // Return false to indicate that an applicant is already present at the specified index
    }
}

これはnewGameメソッドの構造のスケルトンにすぎないため:

public void newGame {
    AllApplicants applicants = new AllApplicants();

    // In order to achieve doing "applicants.applicantArr[0].Id = 5;", you
    // need to do the following.

    // Create a new applicant info with ID as 5
    ApplicantInfo applicant = new ApplicantInfo(5);

    // Add the applicant to the applicant array at index 0
    applicants.addApplicant(applicant, 0);
}

@codeman で言及されているように、配列について読むことに加えて、Java Naming Conventionもご覧になることをお勧めします。

于 2013-03-18T07:46:15.270 に答える