【问题标题】:compare a element from a collection and set it to be last element of that collection比较集合中的元素并将其设置为该集合的最后一个元素
【发布时间】:2015-02-09 23:11:36
【问题描述】:

如何实现:

比较集合中的一个元素(不知道索引,所以可以使用包含/等于来比较字符串值),检查是否存在然后必须向该字符串值添加一些字符(将从其他地方获取) 然后将其添加回集合中作为最后一个元素。

示例场景:

xyz<Collection> contains these values:
"abc"
"hgj"
"jsh"
"yjk"

if (xyz.contains("jsh")){
then concat "jsh" + " " + randomOtherStuff
And put it back in the collection to be last element so when printed the order is 
"abc"
"hgj"
"yjk"
"jsh + " " + randomOtherStuff"
}

提前感谢您的所有帮助:D

【问题讨论】:

  • 你有什么尝试吗?
  • Collections.binarySearch(xyz, "jsh") 将返回索引。

标签: java jakarta-ee arraylist collections


【解决方案1】:
if (collection.contains("jsh")) {
  collection.remove("jsh"); // remove it, so it can be re-added to the end
  collection.add("jsh " + "asdfgdfgsdfhsjdfh"); // add the new string + some random stuff to the end of the list
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2011-04-29
    • 2011-10-28
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2015-12-07
    • 2014-12-23
    相关资源
    最近更新 更多