Create formatted string from ArrayList

前端 未结 7 875
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 20:11

Consider following code:

    ArrayList aList = new ArrayList();
    aList.add(2134);
    aList.add(3423);
    aList.add(4234);
         


        
相关标签:
7条回答
  • 2021-01-04 21:16

    Since Java 8 you can also do:

    ArrayList<Integer> intList = new ArrayList<Integer>();
    intList.add(2134);
    intList.add(3423);
    intList.add(4234);
    intList.add(343);
    
    String prefix = "(";
    String infix = ", ";
    String postfix = ")";
    
    StringJoiner joiner = new StringJoiner(infix, prefix, postfix);
    for (Integer i : intList)
        joiner.add(i.toString());
    
    System.out.println(joiner.toString());
    
    0 讨论(0)
提交回复
热议问题