How to Print an Array in a Set of Objects

advertisements

So, i have a class named Hospital and a method called InsertFolders inside the Hospital class. Inside the method the user must fill an array(max 5) of examinations (I made a class called Folders where i have the set and get methods for the array). Now i have created another method called print where i want to print this array. Note that there is an array of objects which contains the Folders. ListOfFolders[i].getNameOfFolder(this is the field of the folder's name for example). Is there a way where i can print the array? For example when i try ListOfFolders[i].getArrayOfExaminations() it doesn't print the expected examinations.


Just override toString function like:

public class Person{
   private int id;
   private String firstName;
   private String lasrName;
   .
   .

   public Person(){
       this.id = 1;
       this.firstName = "First Name";
       this.lastName = "Last Name";
   }

   public String toString(){
       String str = "";

       str += "Person Info: \n";
       str += "Id : " + id + "\n";
       str += "First Name : " + firstName + "\n";
       str += "Last Name : " + lastName + "\n";
       ....

       return str;
   }   

}

If you would share us your code I would have implement it better, but this is the idea.

Usage:

List<YourClass> list = new ArrayList<YourClass>();
list.add(new Person());

for (YourClass item : list){
      System.out.println(item);  //here it will automaticly use the overridden toString in your class
}

Output:

Person Info:
Id: 1
First Name
Last Name