將List中的某一個元素移動到首位或指定位置


 

1. Collections.swap(list,b,c);(注意:索引為a的元素與索引為b的元素交換位置,其他元素的索引值不變)

  list是你需要換位置的List。

  a是你當前需要換位置的元素的索引。

  b是你需要將這個元素換到哪個索引。

2.list.add(0, list.remove(i));(注意:先移除,然后把移除的元素放在指定位置,指定位置之后元素的索引值往后加1)



 List<String> test = Lists.newArrayList(); test.add("test1"); test.add("test2"); test.add("test3"); System.out.println("更改前:" + JSON.toJSONString(test)); Collections.swap(test, 1, 0); System.out.println("更改后:" + JSON.toJSONString(test));
--------------------------------------------------------------
 Collections.sort(proList, Comparator.comparing(ScenicListResp::getKindsLevel).reversed()); 
if (searchScenicReq.getCityId() == 370400){
// Collections.swap(proList, 1, 0);
proList.add(0, proList.remove(4));
proList.add(1, proList.remove(23));
proList.add(2, proList.remove(3));
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM