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

前端 未结 5 1193
谎友^
谎友^ 2021-01-25 17:08

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

\"genres\": [
      \"Action\",
         


        
相关标签:
5条回答
  • 2021-01-25 17:45

    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
            }
        }
    
    0 讨论(0)
  • 2021-01-25 17:48

    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

    0 讨论(0)
  • 2021-01-25 17:59

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

    0 讨论(0)
  • 2021-01-25 17:59

    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.

    0 讨论(0)
  • 2021-01-25 18:05

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题