Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Overriding toString() of an Array Object

807580Feb 22 2010 — edited Feb 24 2010
Can I override the toString() method of an Array object.
So that a customised message will be printed with System.out.println
String[] strArr={"one","two","three"};
System.out.println(strArr);
--> now it is printing "java.lang.String;@hashcode"

i would like to be print "one two three" (i.e space separated values
of the elements) with the statement System.out.println(strArr);

For this is there any way of overriding toString() of the String[] array object.

Currently I am managing with
static String printArray(Object[] objArr) {
String str="";
for (int i=0; i<objArr.length; i++){
str=str + objArr[i]+" ";
}
return str;
}
and calling as printArray(strArr);
but this is ofcourse a bit tedious.

Thanks in Advance.

Madhu_1980
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2010
Added on Feb 22 2010
18 comments
1,349 views