Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we add String array into StringBuffer or StringBuilder in java?

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.

like image 601
Nithya Avatar asked Aug 24 '26 08:08

Nithya


1 Answers

  • 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).

like image 99
Mena Avatar answered Aug 25 '26 21:08

Mena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!