2015-02-09 69 views
-3

如何實現:比較集合中的元素並將其設置爲該集合的最後一個元素

比較集合中的元素(不知道索引,因此可以使用contains/equals來比較字符串值),檢查是否存在,然後必須爲該字符串值添加一些字符(將從其他地方拾取),然後將其添加回集合中作爲最後一個元素。

示例方案:

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

+3

你有沒有嘗試什麼嗎? – 2015-02-09 23:13:39

+0

Collections.binarySearch(xyz,「jsh」)將返回索引。 – 2015-02-09 23:13:41

回答

3
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 
} 
相關問題