怎样把一个字符复制N次,如 'a'变成'aaaaa'

解决方案 »

  1.   

    public String copy (char ch , int n) {
         if(n<=0){
             return null ; 
         } else {
             StringBuffer sb = new StringBuffer();
             for(int i = 1;i<=n){
                 sb.append(ch);
             } 
             return sb.toString();
          }     
         
     
    }
      

  2.   

    public class StringTest {
        public static void main(String args[])
        {
            String s="a";
            StringBuffer sbu=new StringBuffer(s);
            for(int i=0;i<5;i++)
            {
                sbu.append(s);
            }
            System.out.println(sbu.toString());
        }
    }
      

  3.   

    interpb(曾曾胡)的for循环少写了i++哟,赫赫
      

  4.   

    人家要的是字符也....
    public String repeat(char src, int times)
    {
        char[] a = new char[times];
        for(int i=0;i<times;i++)
        {
            a[i] = src;
        }
        Stirng repeated = new String(a);    return repeated;
    }
      

  5.   

    public String recopy(char a)
    {
      String str=null;
      for(int i =0;i<10;i++)
        {
          str +=a;
        }
    }
      

  6.   

    char c='a';
    char[] charArr=new char[n];
    Arrays.fill(charArr,c);
    return new String(charArr);
      

  7.   

    最有效的方法是长度*2的方式增长
    byte a = 'a';
    byte[] b = new byte[10];

    //初始化
    b[0] = a;
    int pos=1,stopPos=b.length;
    //循环拷贝
    while(pos<stopPos){
    int length = (pos<<1)>stopPos?(stopPos-pos):pos;
    System.arraycopy(b,0,b,pos,length);
    pos *= 2;
    }
      

  8.   

    上面只是简单思路,具体自己完善。如果用String的话用StringBuffer就可以了。
      

  9.   

    public class StringTest {
        public static void main(String args[])
        {
            String s="a";
            StringBuffer sbu=new StringBuffer(s);
            for(int i=0;i<5;i++)
            {
                sbu.append(s);
            }
            System.out.println(sbu.toString());
        }
    }
      

  10.   

    推荐treeroot(旗鲁特)的方法,简单高效,但需要import java.util.Arrays;
    完整用法:import java.util.Arrays;public class DuplicateChars {
      public static String duplicate(char c, int len) {
        char[] ca = new char[len];
        Arrays.fill(ca, c);
        return ca;
      }  //test it
      public static void main(String[] args) {
        System.out.println(DuplicateChars.duplicate('a', 10));
      }}