java - Properly using toString to print array? -
this question has answer here:
i have array need print, , i've looked through stackoverflow know need use tostring
don't print hashcode, reason it's still printing stuff "music2.music2@4162b8ce
, music2.music2@3852fdeb
, music2.music2@509c6c30
"
music2[] musiclist = new music2[10]; musiclist[0] = new music2("pieces of you", "1994", "jewel"); musiclist[1] = new music2("jagged little pill", "1995", "alanis morissette"); musiclist[2] = new music2("what if it's you", "1995", "reba mcentire"); musiclist[3] = new music2("misunderstood", "2001", "pink"); musiclist[4] = new music2("laundry service", "2001", "shakira"); musiclist[5] = new music2("taking long way", "2006", "dixie chicks"); musiclist[6] = new music2("under skin", "2004", "avril lavigne"); musiclist[7] = new music2("let go", "2002", "avril lavigne"); musiclist[8] = new music2("let go", "2007", "tim mcgraw"); musiclist[9] = new music2("white flag", "2004", "dido"); public static void printmusic(music2[] musiclist) { system.out.println(arrays.tostring(musiclist)); }
this array , method using print it. appreciated.
you should define tostring()
method in music2
class , print in way like. don't know how fields in object named exactly, can this:
public class music2 { ... @override public string tostring() { return this.artist + " - "+ this.title + " (" + this.year + ")"; } }
after printmusic
method work expected.
Comments
Post a Comment