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]

Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 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. – Jon Skeet Mar 04 '16 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. – Mahendra Mar 04 '16 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](http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-opposite-of-sp) – sahel Mar 04 '16 at 14:08

6 Answers6

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());
Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
3

simpliest way is;

array_list.toString().replace("[","").replace("]","");
Y.Kaan Yılmaz
  • 612
  • 5
  • 18
  • 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 ? https://docs.oracle.com/javase/7/docs/api/java/lang/String.html – Y.Kaan Yılmaz Mar 04 '16 at 14:16
3

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

    String joinedString = list.stream()
            .map(Object::toString)
            .collect(Collectors.joining(", "));
Peter Zeller
  • 2,245
  • 19
  • 23
1

When using Java 8. See here.

private toStringNoBrackets(ArrayList MyList) {
    return String.join(",", MyList);
}
Community
  • 1
  • 1
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
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();
Jason Portnoy
  • 757
  • 8
  • 23
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