How do I override the toString method to display elements in a list in Java?

后端 未结 5 389
醉梦人生
醉梦人生 2021-01-28 02:18

I have been trying to figure this out for hours, but I am unable find an answer that works.

For completeness, I have posted the entire code below. If I do not Override

5条回答
  •  抹茶落季
    2021-01-28 02:35

    Since you have provide parameter to your toString() method, it is no more the Object's class toString() method. It is a new method and works as a overloaded version of Object's toString() not the overridden version. Remove the parameter. To work the toString() properly you need to have the exactly same method signature -

    public String toString(){
       //implmentation
    }
    

提交回复
热议问题