2
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class Test {
    final static Logger logger = Logger.getLogger(Test.class);
    private static DecimalFormat decimal_inpoints = new DecimalFormat("0.00");

    public static void main(String args[]) throws UnknownHostException,
            ParseException {

        ArrayList<Integer> array_list = new ArrayList<Integer>();

        array_list.add(1);
        array_list.add(0);

        String joinedString = array_list.toString();

        System.out.println(joinedString);

    }

}

How can i get output as 1,0

When i used array_list.toString(); its giving putput as [1,0] (array added )

Could you please tell me How to get 1,0 instead of [1,0]

3
  • 1
    The simplest way is to perform the "joining" operation yourself... have you tried doing that? (Use a StringBuilder, appending an item then a comma, etc.) Also note that your code doesn't do anything that would generate an UnknownHostException or a ParseException, nor do you use the logger or DecimalFormat. All of that can be removed from your code, to make it simpler to see what you're interested in. Commented Mar 4, 2016 at 14:05
  • 1
    This is the default implementation of toString() of ArrayList class. Your have to write your own method to get string in desired format which you can easily do using StringBuilder. Commented Mar 4, 2016 at 14:06
  • You should check this post: http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-opposite-of-sp Commented Mar 4, 2016 at 14:08

6 Answers 6

6

Using Apache Commons Lang:

String join = StringUtils.join(joinList, ",");

Using Java 8

String joined = String.join(",", list);

Using Google Guava (Joiner)

Joiner joiner = Joiner.on(",");
String join = joiner.join(joinList);

Using StringBuilder

StringBuilder builder = new StringBuilder(array_list.size());
boolean isFirst = true;
for (int i : array_list){
  if (isFirst) {
    builder.append(i);
    isFirst = false;
  } else {
    builder.append(", " + i);
  }
}
System.out.println(builder.toString());
Sign up to request clarification or add additional context in comments.

Comments

3

simpliest way is;

array_list.toString().replace("[","").replace("]","");

1 Comment

string.replace() is not exist in java 7 ? I'm really surprised. EDIT: just checked out java 7 docs and I see there is string.replace() method. how is that possible you cannot use it ? docs.oracle.com/javase/7/docs/api/java/lang/String.html
3

In Java 8 you can do it with the joining Collector:

    String joinedString = list.stream()
            .map(Object::toString)
            .collect(Collectors.joining(", "));

Comments

1

When using Java 8. See here.

private toStringNoBrackets(ArrayList MyList) {
    return String.join(",", MyList);
}

Comments

1

You can create your own class and override the toString method.

import java.util.ArrayList;

public class MyIntegerArrayList extends ArrayList<Integer>
{
    @Override
    public String toString(){
        return super.toString().replace("[","").replace("]","");
    }
}


MyIntegerArrayList myIntegerArrayList = new MyIntegerArrayList();
myIntegerArrayList.add(0);
myIntegerArrayList.add(1);
myIntegerArrayList.toString();

Comments

0

Use StringJoiner:

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

public class SOPlayground {

    public static void main(String[] args) {
        List<Integer> array_list = new ArrayList<>();
        array_list.add(1);
        array_list.add(0);

        StringJoiner sj = new StringJoiner(", ");
        array_list.stream().forEach((i) -> {
            sj.add(i.toString());
        });
        System.out.println(sj.toString());
    }

}

Output:

1, 0

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.