/*
* Stitch the array into a string
*/
public class StringBufferTest2 {
public static void Main (string[] args) {
Define an array
Int[] arr = {44, 33, 55, 11, 22};
Defining features
Mode 1: Use String to stitch the way
String S1 = arraytostring (arr);
System.out.println ("S1:" + S1);
Way 2: Use StringBuffer to do the stitching way
String s2 = arrayToString2 (arr);
System.out.println ("s2:" + s2);
}
The way to do stitching with StringBuffer
public static String arrayToString2 (int[] arr) {
StringBuffer sb = new StringBuffer ();
Sb.append ("[");
for (int x = 0; x < arr.length; × x + +) {
if (x = = arr.length-1) {
Sb.append (Arr[x]);
} else {
Sb.append (Arr[x]). Append (",");
}
}
Sb.append ("]");
return sb.tostring ();
}
Use String to stitch the way
public static String arraytostring (int[] arr) {
String s = "";
s + = "[";
for (int x = 0; x < arr.length; × x + +) {
if (x = = arr.length-1) {
s + = arr[x];
} else {
s + = arr[x];
s + = ",";
}
}
s + = "]";
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.