如何在另一个对象数组中打印对象的int值?

时间:2022-09-20 21:43:36

How does one print the private int data field of an object that's within another object's array.

如何打印另一个对象的数组中的对象的私有int数据字段。

So if I had an object called Classroom and another object called Student, how would I print the student ID's of the student objects inside the Classroom object's private array member?

因此,如果我有一个名为Classroom的对象和另一个名为Student的对象,我将如何在Classroom对象的私有数组成员中打印学生对象的学生ID?

Would I override toString within Student to print the studentID? But how do you use that in the Classroom object's array to print out the array of IDs?

我会覆盖Student内的toString来打印studentID吗?但是如何在Classroom对象的数组中使用它来打印出ID数组呢?

3 个解决方案

#1


1  

In your Student class, you should create a method that returns the student's id, like in the example below:

在您的Student课程中,您应该创建一个返回学生ID的方法,如下例所示:

class Student 
{
     private id;

     //... constructor and other code

     int getID() {return this.id;} 
}

In your Classroom class, you should create a method that adds the student to the array of students (I used an ArrayList in this case) and a method that prints the ids of all students in the list. Look below:

在您的课堂课程中,您应该创建一个方法,将学生添加到学生数组(在本例中我使用了ArrayList)以及打印列表中所有学生的ID的方法。往下看:

class Classroom
{
    private ArrayList<Student> studentsList;

    //... constructor and other code

    void addStudent(Student student) {
        this.studentsList.add(student);
    }

    void printStudentsList() {
        for(Student student: this.studentsList) {
            System.out.println(student.getID());
        }
    }
}

Note that it's just one of the ways you can use to achieve what you want. Since you didn't post your code, I improvised with the information you gave.

请注意,这只是您可以用来实现所需内容的方法之一。由于您没有发布您的代码,我使用您提供的信息即兴创作。

#2


0  

I think that you need to create some public methods to use the private attributes. I think you can design Student and Classroom class as following:

我认为你需要创建一些公共方法来使用私有属性。我认为你可以设计学生和课堂课程如下:

class Student
{
    private int studentID;
    //and others private attributes

    public student()
    {
       //write something here to initiate new object
    }
    public int getID()
    {
        return studentID;
    }
    //you can insert others methods here
}
class Classroom
{
   private Student[] studentArray;
   //add constructer here if you need it
   public int getStudentId(int position) //get student ID at `position` in the array
   {
     return studentArray[position].getID();
   }
}
public class Main
{
    public static void main(String[]args)
    {
       Classroom classroom = new Classroom();
       //do something to insert student to array of the `classroom`

       //assume you need to get ID of student in 6th place
       System.out.println(classroom.getStudentID(6));
    }
}

#3


0  

class Student{ private ArrayListids;

class Student {private ArrayListids;

public Student(){

    ids.add("S001");

    ids.add("S002");

}

public ArrayList<String> getID(){

    return this.ids;

}

} class ClassRoom {

class Class Class {

public static void main(String args[]){

    Student s=new Student();

    ArrayList<String>studentID=s.getID();

    for(String id:studentID){

        System.out.println("Student ID :"+id);

    }

}

}

#1


1  

In your Student class, you should create a method that returns the student's id, like in the example below:

在您的Student课程中,您应该创建一个返回学生ID的方法,如下例所示:

class Student 
{
     private id;

     //... constructor and other code

     int getID() {return this.id;} 
}

In your Classroom class, you should create a method that adds the student to the array of students (I used an ArrayList in this case) and a method that prints the ids of all students in the list. Look below:

在您的课堂课程中,您应该创建一个方法,将学生添加到学生数组(在本例中我使用了ArrayList)以及打印列表中所有学生的ID的方法。往下看:

class Classroom
{
    private ArrayList<Student> studentsList;

    //... constructor and other code

    void addStudent(Student student) {
        this.studentsList.add(student);
    }

    void printStudentsList() {
        for(Student student: this.studentsList) {
            System.out.println(student.getID());
        }
    }
}

Note that it's just one of the ways you can use to achieve what you want. Since you didn't post your code, I improvised with the information you gave.

请注意,这只是您可以用来实现所需内容的方法之一。由于您没有发布您的代码,我使用您提供的信息即兴创作。

#2


0  

I think that you need to create some public methods to use the private attributes. I think you can design Student and Classroom class as following:

我认为你需要创建一些公共方法来使用私有属性。我认为你可以设计学生和课堂课程如下:

class Student
{
    private int studentID;
    //and others private attributes

    public student()
    {
       //write something here to initiate new object
    }
    public int getID()
    {
        return studentID;
    }
    //you can insert others methods here
}
class Classroom
{
   private Student[] studentArray;
   //add constructer here if you need it
   public int getStudentId(int position) //get student ID at `position` in the array
   {
     return studentArray[position].getID();
   }
}
public class Main
{
    public static void main(String[]args)
    {
       Classroom classroom = new Classroom();
       //do something to insert student to array of the `classroom`

       //assume you need to get ID of student in 6th place
       System.out.println(classroom.getStudentID(6));
    }
}

#3


0  

class Student{ private ArrayListids;

class Student {private ArrayListids;

public Student(){

    ids.add("S001");

    ids.add("S002");

}

public ArrayList<String> getID(){

    return this.ids;

}

} class ClassRoom {

class Class Class {

public static void main(String args[]){

    Student s=new Student();

    ArrayList<String>studentID=s.getID();

    for(String id:studentID){

        System.out.println("Student ID :"+id);

    }

}

}