编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。

解决方案 »

  1.   


    public class cTest { /**
     * @param args
     */
    public static void main(String[] args) {

         int[]a = new int[] {1,2,3};
         int j = 0;
         StringBuffer buf = new StringBuffer(); 
         if (a.length > 0){      
           for ( j = 0 ; j < a.length-1 ; ++j){
             buf.append(String.valueOf(a[j])+",");
           }
           buf.append(String.valueOf(a[j]));
         }
        
         System.out.print(buf.toString());

    }
      

  2.   

    最简单的写法:int[] itr={1,2,3}
    String s="";
    for(int i:itr){
       s+=","+i;
    }
    s=s.substring(1);
      

  3.   


    public class arrConn
    { public static void main(String[] args)
    {
    Integer[] array;
    array = new Integer[] {1,2,3,4,5,6,7,8,9,0};
    String str = "";
    for (int i=0;i<array.length-1;i++)
    {
    str = str + array[i].toString() + ",";
    }
    str = str + array[array.length-1].toString();
    System.out.println(str);
    }
    }
      

  4.   


    public class intToStr{
    public static void main(String[] args)
    {
    int[] in={1,2,3}
    Sring str=""
    for(int i=0;i<in.length;i++)
    if(str==""){
    str= in[i].toString;
    }else{
    str=str+in[i].toString+",";
    }
    }
    System.out.println(str); }
      

  5.   


    int[] itr={1,2,3} 
    String s="" + itr[0]; 
    for(int i=1; i<itr.length; i++){ 
      s += ","+itr[i]; 

    System.out.println(s);
      

  6.   


    public class TestN
    {
    public static void main(String[] args)
    {
    int[] a = {1,2,3,4,67,8};
    StringBuilder test = new StringBuilder();
    for(int i=0;i<a.length-1;i++)
    {
    test.append(a[i]+",");    //前 n-1 个元素
    }
    test.append(a[a.length-1]);  //将最后一个元素单独加上,解决末尾有","问题
    System.out.println(test);
    }
    }
      

  7.   

    public class ConnectString {
    public static void main(String[] args) { String result = new String();
    if(args.length <= 0) {
    System.out.println(
    "Useage: java ConnectString int[]");
    }
    else {
    for(int i = 0; i < args.length; i ++) {
    result += args[i];
    if(i != args.length - 1)
    result += ",";
    }
    }

    System.out.println(result);
    }
    }
      

  8.   

    public static void main( String[] args )
    {
    int[] arr = { 1, 2, 3, 4, 5, 6, 2, 3, 33, 44 };
    StringBuilder buf = new StringBuilder();
    for ( int i = 0; i < arr.length - 1; i++ )
    {
    buf.append( arr[i] + "," );
    }
    buf.append( arr[arr.length - 1] );
    System.out.println( buf );
    }