简体   繁体   中英

Convert ArrayList of Integers to String Java

I want to convert an ArrayList of Integers to a single string.

For example:

List<Integers> listInt= new ArrayList();

String str = "";

listInt.add(1);
listInt.add(2);
listInt.add(3);

// I want the output to be: str = "123";

String numberString = listInt.stream().map(String::valueOf).collect(Collectors.joining(""));

//You can pass a delimiter if you want to the joining() function, like a comma. ","

Try this, the simplest way:

List<Integer> listInt = Arrays.asList(1,2,3);
String str = "";
StringBuilder builder = new StringBuilder();
for(Integer item : listInt)
  builder.append(item);
str = builder.toString();
System.out.println(str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM