简体   繁体   中英

How to print the int value of an object inside another object array?

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

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?

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?

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

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:

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.

class Student{ private ArrayListids;

public Student(){

    ids.add("S001");

    ids.add("S002");

}

public ArrayList<String> getID(){

    return this.ids;

}

} class ClassRoom {

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

    }

}

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM