java 从 ArrayList 创建格式化字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4671240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:26:39  来源:igfitidea点击:

Create formatted string from ArrayList

javastring

提问by StefanE

Consider following code:

考虑以下代码:

    ArrayList<Integer> aList = new ArrayList<Integer>();
    aList.add(2134);
    aList.add(3423);
    aList.add(4234);
    aList.add(343);

    String tmpString = "(";

    for(int aValue : aList) {
        tmpString += aValue + ",";
    }
    tmpString = (String) tmpString.subSequence(0, tmpString.length()-1) + ")";

    System.out.println(tmpString);

My result here is (2134,3423,4234,343) as expected..

我的结果是 (2134,3423,4234,343) 正如预期的那样..

I do replace the last comma with the ending ) to get expected result. Is there a better way of doing this in general?

我确实用结尾替换了最后一个逗号 ) 以获得预期的结果。一般来说,有没有更好的方法来做到这一点?

回答by Rob Hruska

You could use Commons Lang:

您可以使用Commons Lang

String tmpString = "(" + StringUtils.join(aList, ",") + ")";

Alternatively, if you can't use external libraries:

或者,如果您不能使用外部库:

StringBuilder builder = new StringBuilder("(");
for (int aValue : aList) builder.append(aValue).append(",");
if (aList.size() > 0) builder.deleteCharAt(builder.length() - 1);
builder.append(")");
String tmpString = builder.toString();

回答by MrMeszaros

Since Java 8you can also do:

Java 8 开始,您还可以执行以下操作:

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());

回答by jzd

You will have to replace the last comma with a ')'. But use a StringBuilder instead of adding strings together.

您必须用“)”替换最后一个逗号。但是使用 StringBuilder 而不是将字符串添加在一起。

回答by Aravind Yarram

How about this from google-guava

这个来自google-guava怎么样

String joinedStr = Joiner.on(",").join(aList);

System.out.println("("+JjoinedStr+")");

回答by DanM

Building off Mateusz's Java 8 example, there's an example in the StringJoinerJavaDoc that nearly does what OP wants. Slightly tweaked it would look like this:

构建Mateusz的 Java 8 示例,StringJoinerJavaDoc 中有一个示例几乎可以满足OP 的要求。稍微调整一下,它看起来像这样:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4);

String commaSeparatedNumbers = numbers.stream()
     .map(i -> i.toString())
     .collect( Collectors.joining(",","(",")") );

回答by Qwerky

If you used an Iteratoryou could test hasNext()inside your loop to determine whether you needed to append a comma.

如果您使用了Iterator,则可以hasNext()在循环内进行测试以确定是否需要附加逗号。

StringBuilder builder = new StringBuilder();
builder.append("(");

for(Iterator<Integer> i=aList.iterator(); i.hasNext();)
{
  builder.append(i.next().toString());
  if (i.hasNext()) builder.append(",");
}

builder.append(")");

回答by m.edmondson

for(int aValue : aList) {
    if (aValue != aList.Count - 1)
    {
          tmpString += aValue + ",";
    }
    else
    {
          tmpString += aValue + ")";
    }
}

Perhaps?

也许?