日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java ArrayList 排序 遍历

發(fā)布時間:2024/1/1 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java ArrayList 排序 遍历 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、介紹

ArrayList是實現(xiàn)List接口的動態(tài)數(shù)組,注意,ArrayList實現(xiàn)不是同步的。如果多個線程同時訪問一個ArrayList實例,而其中至少一個線程從結構上修改了列表,那么它必須保持外部同步。

二、操作

初始化:

import java.util.ArrayList; ArrayList<String> webList = new ArrayList<String>();

存:

webList.add("a",); webList.addAll(otherList); //將兩個list連起來

取:

webList.get(0);

是否存在:

webList.contains("a");

是否為空:

webList.isEmpty();

大小:

webList.size();

清空:

webList.clear();

刪除:

webList.remove("a"); //刪對象 webList..remove(1); //根據(jù)index刪

將數(shù)組轉成ArrayList:

ArrayList<String> temp = new ArrayList<String>(Arrays.asList( kmean.split(" ") ) );

三、排序、遍歷

排序:

//對加入的近鄰進行排序,并提取前面的k個 Collections.sort(kmean,new SortByVal()); public static class SortByVal implements Comparator{public int compare(Object o1, Object o2){GenericPair<String,Integer> k1 = (GenericPair<String,Integer>) o1;GenericPair<String,Integer> k2 = (GenericPair<String,Integer>) o2;//升序if(k1.getSecond()>k2.getSecond()){return 1;}else{return -1; //一定返回1的相反數(shù)-1}}} List<Map.Entry<String,Integer>> top = new ArrayList<Map.Entry<String, Integer>>(count.entrySet());Collections.sort(top, new Comparator<Map.Entry<String, Integer>>() {public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {//降序return o2.getValue().compareTo(o1.getValue());} });

遍歷:

for(String item:temp){ }for(int i=index; i<dataset.size(); i++){dataset.get(i); }

總結

以上是生活随笔為你收集整理的java ArrayList 排序 遍历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。