How can I override the toString method of an ArrayList in Java? -


i have own implementation of tostring() method arraylist in java. however, can't working though added tostring() class contains arraylist.

@override public string tostring() {     string result = "+";     (int = 0; < list.size(); i++) {         result += " " + list.get(i);     }     return result; } 

when call arraylist list.tostring(), still default representation. missing something?

you should like

public static string listtostring(list<?> list) {     string result = "+";     (int = 0; < list.size(); i++) {         result += " " + list.get(i);     }     return result; } 

and pass list in argument of listtostring(). can technically extend arraylist (either anonymous class or concrete one) , implement tostring yourself, seems unnecessary here.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -