列表有下標(biāo),是一個可變?nèi)萜鳎乜梢灾貜?fù),Java中l(wèi)ist包含arraylist和linklist,通常使用的是arraylist,查詢速度更快,導(dǎo)入的包是java.util.ArrayList。

定義列表時,如果指定列表接受的數(shù)據(jù)類型為8大數(shù)據(jù)類型,需要使用對應(yīng)數(shù)據(jù)類型的包裝類;列表是一個可變?nèi)萜鳎x后默認(rèn)元素為空。
//創(chuàng)建一個list
Listanimallist = new ArrayList<>();
//創(chuàng)建一個ArrayList,接受數(shù)據(jù)類型為String
ArrayListarlist = new ArrayList();
//創(chuàng)建一個ArrayList,接受數(shù)據(jù)類型為Int
ArrayListbrlist = new ArrayList(); 2、增刪改操作
(1)新增新增主要使用add方法,默認(rèn)在尾部添加,也可以在指定位置添加,addAll方法可以添加整個集合到列表。
Listanimallist = new ArrayList<>();
//默認(rèn)尾部添加
animallist.add("dog");
animallist.add("cat");
animallist.add("fish");
//在指定位置添加
animallist.add(1,"fish");
animallist.add(3,"bird");
animallist.add(5,"cat");
ArrayListarlist = new ArrayList();
//將animallist中的元素添加到arlist
arlist.addAll(animallist); (2)刪除列表中的元素,可以使用remove方法根據(jù)下標(biāo)刪除,也可以根據(jù)值刪除,如果有多個重復(fù)的值,默認(rèn)刪第一個。也可以使用clear方法刪除所有元素。
animallist.remove(1);
animallist.remove("cat");
arlist.clear(); (3)修改列表中的元素,可以使用set方法修改元素值,操作時如果下標(biāo)越界則會報(bào)錯 。也可以使用replaceAll方法將給定的操作內(nèi)容替換掉列表的中每一個元素。
animallist.set(3, "snake");
animallist.set(4, "snake");//執(zhí)行結(jié)果報(bào)錯java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
//replaceAll方法替換掉所有元素
animallist.replaceAll(a ->a +"s");//在所有元素后加上s3、訪問列表列表可以直接輸出;size方法可以獲取到列表的長度,get方法可以通過索引獲取到列表對應(yīng)元素,indexOf方法可以獲取對應(yīng)元素的索引。
// 輸出整個列表
System.out.println(animallist);
// 輸出列表的長度
System.out.println(animallist.size());
// 通過索引獲取列表對應(yīng)元素
System.out.println(animallist.get(1));
// 獲取列表元素的索引值
System.out.println(animallist.indexOf("birds"));輸出結(jié)果為:
遍歷列表主要有3種方式,for循環(huán)遍歷,foreach遍歷,iterator迭代器遍歷。
(1)for循環(huán)遍歷for循環(huán)通過列表的下標(biāo)遍歷。
// for循環(huán)下標(biāo)遍歷
for (int index = 0; index< animallist.size(); index += 2) {System.out.print(animallist.get(index) + " ");
}(2)foreach遍歷foreach直接遍歷列表中的元素。
// foreach遍歷
for (String s : animallist) {System.out.print(s + " ");
}(3)iterator迭代器遍歷//iterator迭代器遍歷
Iteratorlit = animallist.iterator();//聲明list的迭代器
while (lit.hasNext()) {String value = lit.next();
System.out.println(value + " ");
} 5、列表轉(zhuǎn)換列表可以通過對應(yīng)方法直接轉(zhuǎn)為數(shù)組或字符串。toArray將 arraylist 轉(zhuǎn)換為數(shù)組;toString將 arraylist 轉(zhuǎn)換為字符串。
// 列表轉(zhuǎn)為數(shù)組
String[] animals=new String[4];
animallist.toArray(animals);
// 遍歷數(shù)組
for (String i:animals){System.out.println(i);
}
// 列表轉(zhuǎn)為字符串
System.out.println(animallist.toString());輸出結(jié)果為:
列表的一些其他操作包括:
sort方法------對arraylist 元素進(jìn)行排序;
subList方法------截取部分 arraylist 的元素;
isEmpty方法------判斷 arraylist 是否為空;
contains方法------判斷元素是否在 arraylist;
// 升序排列
animallist.sort(Comparator.naturalOrder());
System.out.println(animallist);
// 按照列表索引截取部分列表
System.out.println(animallist.subList(1, 3));//索引區(qū)間左閉右開
// 判斷是否為空
System.out.println(animallist.isEmpty());
// 判斷是否包含某元素
System.out.println(animallist.contains("dogs"));輸出結(jié)果是:
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
文章名稱:Java中列表的基本操作-創(chuàng)新互聯(lián)
文章路徑:http://www.chinadenli.net/article30/dcjhpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、自適應(yīng)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、ChatGPT、網(wǎng)站策劃、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容