Hello.

There is an ArrayList<Character> , how can it be converted to a string and displayed on the screen without commas? In this version:

 System.out.println(massStringList.toString()); 

Commas are inserted after each character. The string itself has its commas, so deleting everything is not an option.

Full view of the line to be displayed:

 String s = "Текст, текст, текст, текст"; char[] massString = s.toCharArray(); int len = massString.length; Character[] array = new Character[len]; for (int i = 0; i < len ; i++) { array[i] = new Character(s.charAt(i)); } ArrayList<Character> massStringList = new ArrayList<>(asList(array)); 

The string is first converted to char[] , and then to Character[] , so that you can make an ArrayList from an array of characters. Crutches too, yes. And it needs exactly ArrayList , just the array does not fit.

I’m interested in how this can be done exactly by the Java API, I can do it through for , but these are some crutches, like for example:

 for (Character chr : massString) { System.out.print(chr); } 

I met here a couple of discussions on a similar topic, but I didn’t see anything other than the said crutches.

  • Please list the question in the list which solutions you consider a crutch. And then you look and there will be nothing to advise - Alexey Shimansky
  • Alexey Shimansky, added. - kamradserg
  • @kamradserg add an example with the contents of the ArrayList - Lex Hobbit
  • What about Java stream api Join ()? - Senior Pomidor
  • @SeniorPomidor, if not difficult, please deploy. - kamradserg

3 answers 3

 String str = String.join("", massStringList); System.out.println(str); 
  • writes cant resolve method - kamradserg
  • @kamradserg this method appeared in Java 8. - Alex78191

You can implement the following translation into the list and back:

 String s = "Текст, текст, текст, текст"; // Convert to list List<Character> list = IntStream.range(0, s.length()) .mapToObj(s::charAt) .collect(Collectors.toList()); // Convert to string String result = list.stream().map(c -> "" + c).collect(Collectors.joining()); System.out.println(result); // Out: Текст, текст, текст, текст 
     StringJoiner sj = new StringJoiner("","Out: ",""); list.forEach(sj::add); // или любой другой цикл. System.out.println(sj.toString());