繁体   English   中英

在Java中将逗号附加到字符数组

[英]Append comma to character array in java

我有一个字符数组。 我想在每个第三个字符后添加逗号(,)。 我尝试了以下代码。

    public class Comma {
    char [] str = {'1','2','3','4','5','6','7','8','9'};
    char [] buf = new char[15];
    int size = str.length;
    int c=1;
    public void insert()
    {
            for(int i=0;i<size;i++)
            {
                  c++;
                  if(c%3==0)
                  {
                    buf[c] = ',';
                    i++;
                  }
                  buf[i]=str[i];
             }  
            System.out.println("Final String");
            for(int i=0;i<buf.length;i++)
            System.out.print(buf[i]);
   }
   public static void main(String args[])
   {
        Comma c = new Comma();
        c.insert();
   }

   }

我得到以下输出:

Final String
1 345 789

有人可以纠正我吗?

这将产生123,456,789

public class MySimpleClass {

    public static void main(String[] args) {        
        char [] str = {'1','2','3','4','5','6','7','8','9'};
        StringBuffer output = new StringBuffer();
        for (int i=1; i<=str.length; i++) {
            output.append(str[i-1]);
            if (i%3==0 && i<str.length) {
                output.append(",");
            }
        }
        System.out.println(output);
    }

}

如果要生成带格式的数字,请阅读此http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

输出:

Final String
123,456,789

这是代码


public class Comma {
    char[] str = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    char[] buf = new char[20];
    int size = str.length;
    int c = 1;

public void insert() { for (int i = 0; i < size; i++) { if (c % 4 == 0) { buf[c - 1] = ','; i--; } else { buf[c - 1] = str[i]; } c++; } System.out.println("Final String"); for (int i = 0; i < buf.length; i++) { System.out.print(buf[i]); } } public static void main(final String args[]) { Comma c = new Comma(); c.insert(); }

}

        int j = 0;
        for(int i=0;i<size;i++)
        {
              c++;
              if(c%3==0)
              {
                buf[j] = ',';
                j++;
              }
              buf[j]=str[i];
              j++;
         }  

请尝试以下方法:

 int c=0;
  for(int i=0;i<size;i++)
  {
       if((i+1)%3==0)
       {
         buf[c] = ',';
         c++;
       }
     buf[c]=str[i];
    c++;
   }  

我假设您要在左侧添加逗号:您需要“ 23,304”,而不是“ 233,04”。

List<Character> chars = new LinkedList<Character>(Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9'));
int positionForNewComma = chars.size() - 3;
while (positionForNewComma > 0) {
chars.add(positionForNewComma, ',');
    positionForNewComma -= 3;
}
System.out.println(chars);

如果从右侧添加逗号,则改为:

List<Character> chars = new LinkedList<Character>(Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9'));
int positionForNewComma = 3;
while (chars.size() > positionForNewComma) {
    chars.add(positionForNewComma, ',');
    positionForNewComma += 3 + 1; // + 1 size the insertion moves the elements on the right
}
System.out.println(chars);

最好避免使用原始数组:[]。 如果确实使用原始数组,则将它们声明为“ char []”而不是“ char []”看起来更像Java。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM