【问题标题】:How to add a comma at the end of every element in ArrayList On Android如何在Android上的ArrayList中的每个元素的末尾添加一个逗号
【发布时间】:2017-08-05 05:43:31
【问题描述】:

在我的应用程序中,我想使用这个 Library 来显示 ArrayList 项目。
来自服务器的我的 ArrayList:

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

我为显示项目编写以下代码:

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);
    }
}

输出如下:

Ganre:动作喜剧家庭

但我想要这样:

Ganre:动作、喜剧、家庭

请帮我解决上面的代码,我是业余爱好者,需要帮助,请帮我解决上面的代码。谢谢大家

【问题讨论】:

  • 建议不要在数组列表中保存 ',' 而是获取流派并在末尾附加 ','。还要检查元素是否是最后一个以防止最后一个','被打印

标签: android arraylist


【解决方案1】:

你能用这样的for循环试试这个吗:

已编辑:

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
        }
    }

【讨论】:

  • 你能用我上面的代码编辑你的代码吗?请用我上面的代码发送给我完整的代码。请问我真的需要你的帮助
  • 您希望在哪个数组列表中用冒号分隔字符串?在 cloudChipList 中还是在 infoSerialFrag_GenreChips 中?
  • infoSerialFrag_GenreChips,请帮帮我我的朋友。我是业余爱好者,真的需要你的帮助。请
  • Jong,你的要求不明确,所以没有得到你想要的。希望我编辑的答案对您有所帮助。
【解决方案2】:

使用TextUtils.join(",", yourList);

【讨论】:

  • 您可以编辑您的代码并将完整代码发送给我吗?请我的朋友
【解决方案3】:

java.util.ArrayList.add(int index, E elemen) 方法插入 此列表中指定位置的指定元素 E。它移动 当前在该位置的元素(如果有)和任何后续 右侧的元素(将为其索引加一)。

已编辑

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

OP将是

Ganre:动作,喜剧,家庭

【讨论】:

  • 如果使用这个,给我看这个:Ganre:动作,喜剧,家庭,!!!!我不想显示,持续到列表中
  • @Jong 获取最后一个值infoSerialFrag_GenreChips.get(infoSerialFrag_GenreChips.size()-1); 然后删除(字符串删除),
  • 在哪里添加您的代码?你可以编辑你的答案并用我上面的代码发送给我完整的代码吗?拜托,我真的需要你的帮助。请
  • 我在我的请求响应中显示了这个!我在您的帮助下编辑了我的代码,但向我展示了这样的内容:Action , Comedy , Family , .我希望删除持续,并像这样向我展示:动作、喜剧、家庭。如何编辑我的代码?请帮帮我我的朋友
【解决方案4】:

使用旧的 Java 6 方式并自己添加逗号,除非它是列表中的最后一个元素。

请看下面的例子:

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);
    }
}

【讨论】:

  • 如果使用这个,给我看这个:Ganre:动作,喜剧,家庭,!!!!我不想显示,持续到列表中
【解决方案5】:

虽然已经晚了,但万一有人来这个帖子…… 如果您使用的是 Java 8,则可以使用如下所示的流式处理和收集器接口:

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);

这里Collectors.joining 在结果中添加了分隔符、前缀和后缀,但它也有一个只有分隔符的选项。

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多