Java中ArrayList的使用詳細介紹
1.ArrayList類
1.1ArrayList類概述
在java中,我們會經(jīng)常使用集合,集合是我們Java SE中最為重要的一個模塊,當我們在創(chuàng)建一個List集合的時候,往往就會使用????new ArrayList()??
?;因此,這個是特別重要的,本文給大家詳細講述該相關的知識點,并且會通過大量的案例加以說明。
- 什么是集合
提供一種存儲空間可變的存儲模型,存儲的數(shù)據(jù)容量可以發(fā)生改變
- ArrayList集合的特點
底層是數(shù)組實現(xiàn)的,長度可以變化
- 泛型的使用
用于約束集合中存儲元素的數(shù)據(jù)類型
1.2ArrayList類常用方法
我們可以通過? ?上篇文章??學習的API去查看ArryList的相關用法以及解釋,剛好的一個練習使用API的機會。
1.2.1構造方法
方法名 | 說明 |
public ArrayList() | 創(chuàng)建一個空的集合對象 |
1.2.2成員方法
方法名 | 說明 |
public boolean remove(Object o) | 刪除指定的元素,返回刪除是否成功 |
public E remove(int index) | 刪除指定索引處的元素,返回被刪除的元素 |
public E set(int index,E element) | 修改指定索引處的元素,返回被修改的元素 |
public E get(int index) | 返回指定索引處的元素 |
public int size() | 返回集合中的元素的個數(shù) |
public boolean add(E e) | 將指定的元素追加到此集合的末尾 |
public void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
1.2.3示例代碼
public class ArrayListDemo02 { public static void main(String[] args) { //創(chuàng)建集合 ArrayList<String> array = new ArrayList<String>(); //添加元素 array.add("hello"); array.add("51CTO"); array.add("一計之長"); //public boolean remove(Object o):刪除指定的元素,返回刪除是否成功 // System.out.println(array.remove("world")); // System.out.println(array.remove("javaee")); //public E remove(int index):刪除指定索引處的元素,返回被刪除的元素 // System.out.println(array.remove(1)); //IndexOutOfBoundsException // System.out.println(array.remove(3)); //public E set(int index,E element):修改指定索引處的元素,返回被修改的元素 // System.out.println(array.set(1,"javaee")); //IndexOutOfBoundsException // System.out.println(array.set(3,"javaee")); //public E get(int index):返回指定索引處的元素 // System.out.println(array.get(0)); // System.out.println(array.get(1)); // System.out.println(array.get(2)); //System.out.println(array.get(3)); //public int size():返回集合中的元素的個數(shù) System.out.println(array.size()); //輸出集合 System.out.println("array:" + array); } }
???那些注掉的需要大家自行測試???,給大家留了一個實踐的小機會,現(xiàn)在放開的執(zhí)行結果如下:
1.3ArrayList存儲字符串并遍歷
1.3.1案例需求
創(chuàng)建一個存儲字符串的集合,存儲3個字符串元素,使用程序?qū)崿F(xiàn)在控制臺遍歷該集合。
根據(jù)該需求,我們給出如下的解題思路:
1:創(chuàng)建集合對象
2:往集合中添加字符串對象
3:遍歷集合,首先要能夠獲取到集合中的每一個元素,這個通過???get(int index)?
??方法實現(xiàn)
4:遍歷集合,其次要能夠獲取到集合的長度,這個通過size()方法實現(xiàn)
5:遍歷集合的通用格式
1.3.2代碼實現(xiàn)
根據(jù)該需求和思路,我們給出以下的實現(xiàn)代碼:
public class ArrayListTest01 { public static void main(String[] args) { //創(chuàng)建集合對象 ArrayList<String> array = new ArrayList<String>(); //往集合中添加字符串對象 array.add("一計之長"); array.add("左冷禪"); array.add("風清揚"); //遍歷集合,其次要能夠獲取到集合的長度,這個通過size()方法實現(xiàn) // System.out.println(array.size()); //遍歷集合的通用格式 for(int i=0; i<array.size(); i++) { String s = array.get(i); System.out.println(s); } } }
根據(jù)該需求我們代碼的運行結果如下:
1.4ArrayList存儲學生對象并遍歷
1.4.1案例需求
創(chuàng)建一個存儲學生對象的集合,存儲3個學生對象,使用程序?qū)崿F(xiàn)在控制臺遍歷該集合。
具體我們通過該需求給出相應的實現(xiàn)思路:
1:定義學生類
2:創(chuàng)建集合對象
3:創(chuàng)建學生對象
4:添加學生對象到集合中
5:遍歷集合,采用通用遍歷格式實現(xiàn)
1.4.2代碼實現(xiàn)
根據(jù)該需求和思路,我們給出以下的實現(xiàn)代碼,首先我們給出Student相應的代碼,具體如下:
/* 學生類 */ public class Student { private String name; private int age; public Student() {} public Student(String name,int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } } 接著給出該需求相應的實現(xiàn)代碼: public class ArrayListTest02 { public static void main(String[] args) { //創(chuàng)建集合對象 ArrayList<Student> array = new ArrayList<>(); //創(chuàng)建學生對象 Student s1 = new Student("一計之長", 5); Student s2 = new Student("51CTO", 12); Student s3 = new Student("stefan", 26); //添加學生對象到集合中 array.add(s1); array.add(s2); array.add(s3); //遍歷集合,采用通用遍歷格式實現(xiàn) for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "," + s.getAge()); } } }
根據(jù)該需求我們代碼的運行結果如下:
1.5ArrayList存儲學生對象并遍歷升級版
1.5.1案例需求
創(chuàng)建一個存儲學生對象的集合,存儲3個學生對象,使用程序?qū)崿F(xiàn)在控制臺遍歷該集合 學生的姓名和年齡來自于鍵盤錄入。
根據(jù)該需求,我們給出以下的思路:
1:定義學生類,為了鍵盤錄入數(shù)據(jù)方便,把學生類中的成員變量都定義為???String?
??類型
2:創(chuàng)建集合對象
3:鍵盤錄入學生對象所需要的數(shù)據(jù)
4:創(chuàng)建學生對象,把鍵盤錄入的數(shù)據(jù)賦值給學生對象的成員變量
5:往集合中添加學生對象
6:遍歷集合,采用通用遍歷格式實現(xiàn)
1.5.2代碼實現(xiàn)
根據(jù)該需求以及思路,我們給出相應的代碼實現(xiàn):
public class ArrayListTest { public static void main(String[] args) { //創(chuàng)建集合對象 ArrayList<Student> array = new ArrayList<Student>(); //為了提高代碼的復用性,我們用方法來改進程序 addStudent(array); addStudent(array); addStudent(array); //遍歷集合,采用通用遍歷格式實現(xiàn) for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "," + s.getAge()); } } /* 兩個明確: 返回值類型:void 參數(shù):ArrayList<Student> array */ public static void addStudent(ArrayList<Student> array) { //鍵盤錄入學生對象所需要的數(shù)據(jù) Scanner sc = new Scanner(System.in); System.out.println("請輸入學生姓名:"); String name = sc.nextLine(); System.out.println("請輸入學生年齡:"); String age = sc.nextLine(); //創(chuàng)建學生對象,把鍵盤錄入的數(shù)據(jù)賦值給學生對象的成員變量 Student s = new Student(); s.setName(name); s.setAge(age); //往集合中添加學生對象 array.add(s); } }
我們將該代碼執(zhí)行,執(zhí)行的結果如下:
總結
我們需要對其多加練習,只要好好的練習,才會真正的掌握相關的應用,并且利用空余時間多讀別人寫的代碼以及源碼才可以更好的應用自如。
到此這篇關于Java中ArrayList的使用詳細介紹的文章就介紹到這了,更多相關Java中的ArrayList內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot如何實現(xiàn)一個實時更新的進度條的示例代碼
本文詳細的介紹了SpringBoot如何實現(xiàn)一個實時更新的進度條,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05利用數(shù)組實現(xiàn)棧(Java實現(xiàn))
這篇文章主要為大家詳細介紹了利用數(shù)組實現(xiàn)棧,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09SpringBoot開發(fā)實戰(zhàn)系列之動態(tài)定時任務
在我們?nèi)粘5拈_發(fā)中,很多時候,定時任務都不是寫死的,而是寫到數(shù)據(jù)庫中,從而實現(xiàn)定時任務的動態(tài)配置,下面這篇文章主要給大家介紹了關于SpringBoot開發(fā)實戰(zhàn)系列之動態(tài)定時任務的相關資料,需要的朋友可以參考下2021-08-08nacos配置中心遠程調(diào)用讀取不到配置文件的解決
這篇文章主要介紹了nacos配置中心遠程調(diào)用讀取不到配置文件的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01Java實現(xiàn)將PPT轉(zhuǎn)為OFD過程詳解
本文將通過Java后端程序代碼展示如何實現(xiàn)將PPT幻燈片轉(zhuǎn)成OFD格式,文中的示例代碼講解詳細,對我們學習或工作有一定的幫助,需要的可以參考一下2022-01-01Spring Boot使用Redisson實現(xiàn)滑動窗口限流的項目實踐
滑動窗口限流是一種流量控制策略,用于控制在一定時間內(nèi)的請求頻率,本文主要介紹了Spring Boot使用Redisson實現(xiàn)滑動窗口限流的項目實踐,具有一定的參考價值,感興趣的可以了解一下2024-03-03