Java List集合方法及遍歷過(guò)程代碼解析
集合元素框架
public class ListDemo02 { public static void main(String[] args) { //創(chuàng)建集合對(duì)象 List<String> list = new ArrayList<String>(); //添加元素 list.add('hello'); list.add('world'); list.add('java'); //輸出集合對(duì)象 System.out.println(list); //[hello, world, java] }}
方法運(yùn)行實(shí)例
//void add(int index, E element) : 在此集合中的指定位置插入指定的元素 list.add(1,'javaee'); //[hello, javaee, world, java] list.add(11,'j2ee'); //IndexOutOfBoundsException//E remove(int index):刪除指定索引處的元素,返回被刪除的元素 System.out.println(list.remove(1)); /*world[hello, java] *///E set(int index,E element):修改指定索引處的元素,返回被修改的元素 System.out.println(list.set(1,'javaee')); /*world[hello, javaee, java] *///E get(int index):返回指定索引處的元素 System.out.println(list.get(1)); /*world[hello, world, java] */
//用for循環(huán)改進(jìn)遍歷 for (int i = 0; i<list.size();i++){ String s = list.get(i); System.out.println(s); } /* hello world java [hello, world, java] */
List集合是帶索引的集合,要考慮越界問(wèn)題。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解Java內(nèi)部類——匿名內(nèi)部類2. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式3. python pymysql鏈接數(shù)據(jù)庫(kù)查詢結(jié)果轉(zhuǎn)為Dataframe實(shí)例4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟6. 使用Python和百度語(yǔ)音識(shí)別生成視頻字幕的實(shí)現(xiàn)7. 教你如何寫出可維護(hù)的JS代碼8. IDEA版最新MyBatis程序配置教程詳解9. xml中的空格之完全解說(shuō)10. css代碼優(yōu)化的12個(gè)技巧
