2013-03-18 97 views
0

我是Java新手。這是一個錯誤。對象變量的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中?

+0

什麼是錯誤 – PSR 2013-03-18 05:23:51

回答

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工作。看看this

,這裏也是一個tutorial讓你開始。

4

Java和C數組之間的區別在於C初始化數組中的所有值,而Java將它們設置爲null。因此,當您致電

applicants.applicantArr[0].Id = 5; 

您將收到NullPointerException,因爲applicants.applicantArr [0]爲null。 你需要創建一個新的applicantInfo,放入數組訪問之前:

allApplicants applicants = new allApplicants(); 
applicants.applicantArr[0] = new applicantInfo(); 
applicants.applicantArr[0].Id = 5; 
0

我會建議與意見和待辦事項代碼的高級結構,你可以填寫詳細信息。最後一部分,我建議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