【问题标题】:Java array of an object variable change对象变量的Java数组变化
【发布时间】:2013-03-06 09:35:20
【问题描述】:

我是 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 中做到这一点?

【问题讨论】:

    标签: java arrays object


    【解决方案1】:

    我会建议您的代码使用 cmets 和 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

    【讨论】:

      【解决方案2】:

      您需要在 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 可以帮助您入门。

      【讨论】:

        【解决方案3】:

        Java 和 C 数组之间的区别在于 C 初始化 Array 中的所有值,而 Java 将它们设置为 null。所以当你打电话时

        applicants.applicantArr[0].Id = 5;
        

        您将收到 NullPointerException,因为申请人.applicantArr[0] 为空。 您需要创建一个新的申请者信息并将其放入数组中,然后才能访问它:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-30
          • 1970-01-01
          • 2015-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多