10

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..

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

7 Answers 7

17

You could use 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();
Sign up to request clarification or add additional context in comments.

3 Comments

I prefer the Commons Lang approach as well, especially as I have it as a dependency in all projects anyway.
I usually end up using it for one reason or another as well. No reason to reinvent the wheel unless you have specific constraints restricting you from using external libraries. Some of the Apache Commons libraries have their drawbacks when it comes to collections and generics, but StringUtils is pretty straightforward.
Very neat solution! I knew there must be a way to do this in a one liner.. And the StringUtils lib is almost standard to include anyway.
8

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

Comments

2

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

Comments

2

How about this from google-guava

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

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

Comments

1

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

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

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

Comments

0

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

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

Comments

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

Perhaps?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.