首页 > 代码库 > StringBuilder===练习

StringBuilder===练习

public class StringDemo9{
    public static void main(String[]args){
        int [] arr={21,43,56,2567,43};
        /**将int数组变成字符串*/
        String s=method1(arr);
        String s1 = method2(arr);
        System.out.println(s);
        System.out.println(s1);
    }
    public static String method1(int[]arr){
        String str = "[";
        for(int x = 0;x<arr.length;x++){
            if(x!=arr.length-1){
                str=str+(arr[x]+",");
            }else{
                str=str+(arr[x]+"]");
            }
        }
        return str;
    }
    public static String method2(int[]arr){
        StringBuilder s = new StringBuilder();
        s.append("[");
        for(int x = 0;x<arr.length;x++){
            if(x!=arr.length-1){
            s.append(arr[x]+",");
            }else{
                s.append(arr[x]+"]");
                }
            
        }
        return s.toString();
    }
}

 

StringBuilder===练习