2

In my application I want use this Library for show ArrayList items.
My ArrayList from server:

"genres": [
      "Action",
      " Comedy",
      " Family"
    ]

I write below code for show Items:

private String[] mostlyMatchedKeywordsStrings;
    private List<String> cloudChipList = new ArrayList<>();
mostlyMatchedKeywordsStrings = serialResponse.getData().getGenres();
                for (String str : mostlyMatchedKeywordsStrings) {
                    cloudChipList.add(str);
                    if (cloudChipList.size() > 0) {
                        infoSerialFrag_GenreChips.addChip(str);
                    }
                }

show me such as this:

Ganre : Action Comedy Family

But I want show me such as this :

Ganre : Action , Comedy , Family

TofferJ
  • 4,678
  • 1
  • 37
  • 49
Jong
  • 65
  • 2
  • 8

3 Answers3

5

You are looking for String.join()

List<String> foo = new ArrayList<>();
foo.add("foo");
foo.add("bar");
foo.add("baz");
System.out.println(String.join(", ", foo)); 
baao
  • 71,625
  • 17
  • 143
  • 203
1

You can either convert to a List and use String.join(", ", list), or you can use a StringJoiner:

StringJoiner sj = new StringJoiner(", ");
for (String s : mostlyMatchedKeywordsStrings) {
    sj.add(s);
}
String joined = sj.toString();

Note that this requires Java 8; if you're using Java < 8, you would have to do something similar to the answer by Professor901.

whitfin
  • 4,539
  • 6
  • 39
  • 67
0

Generally speaking you could do the following:

String[] items = { "Foo", "Bar" };
StringBuilder result = new StringBuilder();
for(int i = 0; i < items.length;) {
    result.append(items[i]);

    if(++i < items.length)
        result.append(", ");
}
String str = result.toString(); // "Foo, Bar"

It's the same for ArrayLists, but you would list.get(i) instead of items[i]:

ArrayList<String> list= new ArrayList<String>();
list.add("Foo"); list.add("Bar");
StringBuilder result = new StringBuilder();
for(int i = 0; i < list.size(); ) {
    result.append(list.get(i));

    if(++i < list.size())
        result.append(", ");
}
String str = result.toString(); // "Foo, Bar"
eliaspr
  • 302
  • 3
  • 15
  • Incrementing `i` in the middle of the loop (and inside the condition of an if) makes it harder to understand that it should – litelite Aug 04 '17 at 17:10
  • Yeah, but it's probably faster. You could also increment `i` at the top of the loop and use `i < items.length - 1` in the if-statement. – eliaspr Aug 04 '17 at 17:12
  • I don't see how it could possibly be faster than putting it at the top of the loop. Also, `i++` returns the value as it was before the incrementation so your `if` is equivalent to comparing to `i`  and **then** incrementing. https://stackoverflow.com/a/2315717/3072566 – litelite Aug 04 '17 at 17:15
  • Also, i actually tested it and it does not work. An extra , is added at the end. – litelite Aug 04 '17 at 17:21
  • Change `i++` to `++i` – eliaspr Aug 04 '17 at 19:08
  • can you edit your answer and send send to me full code with my above codes? please – Jong Aug 04 '17 at 19:40
  • The code in my answer works as it is, i've tested it. I've added the arraylist code, so you just have to copy it and modify the variables used. – eliaspr Aug 04 '17 at 19:55