有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    ^{}类中有几个有用的^{}^{}方法

    String[] strings = { "foo", "bar", "waa" };
    System.out.println(Arrays.toString(strings)); // [foo, bar, waa]
    

    另一种方法是自己在上面loop,然后分别打印每个项目

  2. # 2 楼答案

    使用Apache Commons Lang

    org.apache.commons.lang.StringUtils.join(Arrays.asList(strings), ", ");
    

    使用Spring核心:

    org.springframework.util.StringUtils.collectionToDelimitedString(Arrays.asList(strings), ", ");
    
  3. # 3 楼答案

    您可以使用for循环来完成

    下面是一个例子:

      String[] colors = {"red","blue","black","green","yellow"};
      for (String color : colors) {
       System.out.println(color);
      }
    

    同时检查:What's the simplest way to print a Java array?

    正如Esko在上述链接中所引用的,这是最好的答案:

    In Java 5 Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays.

    Note that Object[] version calls .toString() of each object in array. If my memory serves me correct, the output is even decorated in the exact way you're asking.