Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Now I am developing a simple project on Java.In that I include String Builder to append and print strings on console.When I am trying to append string array into string Builder,it prints object instead of array of strings on console.any one please tell whether my try is right or wrong.

share|improve this question
1  
post your code with your story – amit bhardwaj Oct 7 '14 at 11:22
    
what is your expected output. – yogesh prajapati Oct 7 '14 at 11:24
up vote 1 down vote accepted
  • Add Arrays.toString(yourArray) if you want to add the representation of your String array.
  • If it's a multiple-dimension array (e.g. a String[][]), then add Arrays.deepToString(yourArray) to your StringBuilder.
  • Adding the array itself will add the default String representation.
  • Finally, if you need control over how you represent your array to String, you'll need to iterate the elements yourself and add them to the StringBuilder in a customized format.

Edit

As a side-note, to make this work with arrays of custom Objects, you'll probably want to override the Object.toString method, otherwise your elements will be printed with the default String representation of Object (i.e. this).

share|improve this answer
    
Thanks for the reply.Its working.. – Nithya Oct 8 '14 at 7:02
    
@Nithya you're welcome! Also see my edit on custom Objects. – Mena Oct 8 '14 at 8:40

Array is an Object. Do not directly append the array. Just iterate on array and append each element of array to your String builder.

for(String str : arr) {
    builder.append(str);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.