toString java achieve a method, a convert integer array into a string, for example an array of {1, 2, 3}, returned string "[1, 2, 3]", note position and number of the comma.

import java.util.Arrays;
public class Test42{
    public static void main(String[] args){
        int[] a={1,2,3,4};
        String sum=toString(a);
        System.out.println(sum);
        
    }public static String toString(int[] arr){
        String ret="["; 
        int i;
        for(i=0;i<arr.length;i++){
            if(i<arr.length-1){
                ret+=arr[i]+" , ";
            }else if(i==arr.length-1){
                ret+=arr[i]+"]";
            }
            
        }return ret;
    }
}
Published 87 original articles · won praise 2 · Views 712

Guess you like

Origin blog.csdn.net/Nabandon/article/details/103764265