Jong
Jong

Reputation: 65

How to add a comma at the end of every element in ArrayList On Android

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 = serialResponse.getData().getGenres();
private List<String> cloudChipList = new ArrayList<>();

for (String str : mostlyMatchedKeywordsStrings) {
    cloudChipList.add(str);

    if (cloudChipList.size() > 0) {
       infoSerialFrag_GenreChips.addChip(str);
    }
}

Gives oputput as:

Ganre : Action Comedy Family

But I want it like this :

Ganre : Action , Comedy , Family

Please help me with my above codes, I am amateur and need this help, please help me with my above codes. Thanks all <3

Upvotes: 1

Views: 1570

Answers (5)

Memin
Memin

Reputation: 4090

Although it is late, just case if someone else comes to this thread... If you are using Java 8, you can use streaming and Collectors interfaces like the following:

List<String> cloudChipList = new ArrayList<String>();
    cloudChipList.add("Action");
    cloudChipList.add("Comedy");
    cloudChipList.add("Family");

    String result = cloudChipList.stream().collect(Collectors.joining(" , ", "Genre: ", "\n"));
    System.out.println(result);

Here Collectors.joining adds delimiter, prefix and suffix to the result, but it has an option with only the delimter, too.

Upvotes: 0

Snehal Gongle
Snehal Gongle

Reputation: 337

Do it the old Java 6 way and add the comma yourself unless its the last element in the list.

See the following example:

private String[] mostlyMatchedKeywordsStrings = serialResponse.getData().getGenres();
private List<String> cloudChipList = new ArrayList<>();

for (int i = 0; i < mostlyMatchedKeywordsStrings.length; i++) {

    String str = mostlyMatchedKeywordsStrings[i];

    if (i + 1 < mostlyMatchedKeywordsStrings.length) str = str + ", ";

    cloudChipList.add(str);

    if (cloudChipList.size() > 0) {
       infoSerialFrag_GenreChips.addChip(str);
    }
}

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

The java.util.ArrayList.add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).

Edited

 if (cloudChipList.size() > 0) 
 {
        if(cloudChipList.get(cloudChipList.size()-1).contains(","))
        {
            infoSerialFrag_GenreChips.add(str);
        }
        else
        {
            infoSerialFrag_GenreChips.add(str+",");
        } 
 }  

OP will be

Ganre : Action , Comedy , Family

Upvotes: 0

Shruti
Shruti

Reputation: 815

Can you try this using for loop like this:

EDITED :

private String[] mostlyMatchedKeywordsStrings;
    private List<String> cloudChipList = new ArrayList<>();

    mostlyMatchedKeywordsStrings = serialResponse.getData().getGenres();

    for (String str : mostlyMatchedKeywordsStrings) {
        cloudChipList.add(str);
    }
    if (cloudChipList.size() > 0) {
        for (int i = 0; i < cloudChipList.size(); i++) {

            if (i == (cloudChipList.size() - 1)) //true only for last element
                infoSerialFrag_GenreChips.add(cloudChipList.get(i));

            else
                infoSerialFrag_GenreChips.add(cloudChipList.get(i) + ","); //this will execute for 1st to 2nd last element
        }
    }

Upvotes: 3

Bob
Bob

Reputation: 13865

Use TextUtils.join(",", yourList);

Upvotes: 2

Related Questions