【问题标题】:Splitting an array with commas to separate each elemnt用逗号分割数组以分隔每个元素
【发布时间】:2015-03-10 02:00:54
【问题描述】:

我有一个打印出来的输出:1 2 3 4 5
我希望输出为:1,2,3,4,5
当我打印我的最终数组时,它看起来像: System.out.println(D);
我应该添加什么来满足我的需要。
欢迎所有答案。

【问题讨论】:

  • 如果你有: String t = "1 2 3 4 5 6 7 8";你可以做 String v = t.replace(" ", ",");和 System.out.println(v);它会给你:1,2,3,4,5,6,7,8

标签: java arrays element


【解决方案1】:

为什么不简单地使用 Arrays.toString

public static String toString(int[] a) 返回一个字符串表示 指定数组的内容。字符串表示 由数组元素的列表组成,用正方形括起来 括号(“[]”)。相邻元素之间用字符“, "(逗号后跟一个空格)。元素转换为字符串为 通过 String.valueOf(int)。如果 a 为空,则返回“空”。参数:a - 要返回其字符串表示形式的数组 返回:一个字符串 自的表示: 1.5

试试

System.out.println (Arrays.toString (D));

如果不需要空格,则可以使用replaced

System.out.println (Arrays.toString (D).replace(" ", ""));

【讨论】:

  • 因为 OP 不希望“,”作为分隔符,而只是“,”。使用Arrays.toString 会使代码在未来更难更改。
  • @Atsby 注意到您对空间的评论并为此提供了解决方案。通过使用标准库调用,它比手工编写的代码更少容易出错。
【解决方案2】:

(typeof x)替换为数组的元素类型(或将此代码放在通用函数中以获得奖励积分,但它不适用于原始类型):

StringBuilder out = (new StringBuilder());

boolean first = true;

for ((typeof x) x : D) {
  if (!first) { 
    out.append(",")
  }

  out.append(x.toString());

  first = false;
}

return out.toString();

【讨论】:

  • 这也适用于数组吗?有没有更简单的方法可以添加到我的 system.out.print 中?
  • 是的,它适用于数组(D 是数组)。相同的代码也适用于任何Enumerable 类型,包括例如ArrayList
【解决方案3】:

您可以创建自己的方法来打印结果,例如这个:

    for (int i = 0; i < D.length; i++) {
        System.out.print(D[i]);
        if (i != D.length-1){
            System.out.print(",");
        }
    }

【讨论】:

    【解决方案4】:

    我认为您正在尝试用逗号而不是空格打印出一个数组。

    for (int i = 0; i < arr.length() - 1; i++) {
        System.out.print(arr[i]);
        System.out.print(',');
    }
    System.out.println(arr[arr.length() - 1]);
    

    【讨论】:

      【解决方案5】:

      您可以单独打印出数组的元素,如下所示:

      String output = "";
      //go through all elements in D
      for (int i =0;i<D.length;i++){
          //add the Integer to the String
          output = output+i;
          //add , if not the last element
          if (i<D.length-1){
              output = output+",";
          }
      }
      //Print it out
      System.out.println(output);
      

      【讨论】:

        【解决方案6】:

        试试这个代码:

        如果你的数据类型是整数数组:

        int my_array={1,2,3,4,5};
        
        for(int i=0; i < my_array.length; i++) {
            System.out.print(my_array[i] + ",");//this line will print the value with a coma (,)
        }
        

        如果你的数据类型是字符串;

        String my_number="1 2 3 4 5";
        
        for(int i=0; i < my_number.length; i++){
            if(my_number.toCharArray()[i]!=' ')
               System.out.print(my_number.toCharArray()[i]+",");
        }
        

        或者,

        String my_number="1 2 3 4 5";
        my_number = my_number.replace(' ', ',');//this method (replace) will replace all space(' ') by coma(',')
        System.out.println(my_number);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-24
          • 2011-10-03
          • 1970-01-01
          • 2012-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-27
          相关资源
          最近更新 更多