package StringPackage;
public class StringChange {
/*
* 将int数组变成字符数组
*/
public static void main(String[] args) {
int arr[] = { 3, 5, 2, 1, 5, 7, 6, 5, 3 };
String s = arryToString(arr);
System.out.println(arryToString2(arr));
System.out.println(s);
System.out.println(arryToString3(arr));
}
public static StringBuffer arryToString3(int arr[]) {
StringBuffer str=new StringBuffer();
str.append("[");
for (int i = 0; i < arr.length; i++) {
if(i!=arr.length-1){
str.append(arr[i]+",");
}
else{
str.append(arr[i]+"]");
}
}
return str;
}
public static String arryToString2(int arr[]) {
StringBuffer str=new StringBuffer();//new StringBuffer()代表创建了一个StringBuffer对象,既然是创建对象,肯定有对象的起始地址,然后将地址赋值给str引用型变量,所以就是说str指向了一个StringBuffer的实例对象
str.append("[");
for (int i = 0; i < arr.length; i++) {
if(i!=arr.length-1){
str.append(arr[i]+",");
}
else{
str.append(arr[i]+"]");
}
}
return str.toString();
}
private static String arryToString(int arr[]) {
String str="[";
for (int i = 0; i < arr.length; i++) {
if(i!=arr.length-1){
str+=arr[i]+",";
}
else{
str+=arr[i]+"]";
}
}
return str;
}
}
|
|