Java基礎(chǔ)學(xué)習(xí)之ArrayList類概述與常用方法
一、ArrayList類概述
什么是集合:
提供一種存儲空間可變的存儲模型,存儲的數(shù)據(jù)容量可以發(fā)生改變
ArrayList集合的特點(diǎn):
底層是數(shù)組實(shí)現(xiàn)的,長度可以變化
泛型的使用:
用于約束集合中存儲元素的數(shù)據(jù)類型
二、ArrayList類常用方法
構(gòu)造方法
| 方法名 | 說明 |
| public ArrayList() | 創(chuàng)建一個(gè)空的集合對象 |
成員方法
| 方法名 | 說明 |
| 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() | 返回集合中的元素的個(gè)數(shù) |
| public boolean add(E e) | 將指定的元素追加到此集合的末尾 |
| public void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
示例代碼:
public class ArrayListDemo02 {
public static void main(String[] args) {
//創(chuàng)建集合
ArrayList<String> array = new ArrayList<String>();
//添加元素
array.add("hello");
array.add("world");
array.add("java");
//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));
//public int size():返回集合中的元素的個(gè)數(shù)
System.out.println(array.size());
//輸出集合
System.out.println("array:" + array);
}
}
三、ArrayList存儲字符串并遍歷
需求:創(chuàng)建一個(gè)存儲字符串的集合,存儲3個(gè)字符串元素,使用程序?qū)崿F(xiàn)在控制臺遍歷該集合
思路:
1、創(chuàng)建集合對象
2、往集合中添加字符串對象
3、遍歷集合,首先要能夠獲取到集合中的每一個(gè)元素,這個(gè)通過get(int index)方法實(shí)現(xiàn)
4、遍歷集合,其次要能夠獲取到集合的長度,這個(gè)通過size()方法實(shí)現(xiàn)
5、遍歷集合的通用格式
代碼實(shí)現(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("王五");
//遍歷集合,其次要能夠獲取到集合的長度,這個(gè)通過size()方法實(shí)現(xiàn)
System.out.println(array.size());
//遍歷集合的通用格式
for(int i=0; i<array.size(); i++) {
String s = array.get(i);
System.out.println(s);
}
}
}
四、ArrayList存儲學(xué)生對象并遍歷
需求:創(chuàng)建一個(gè)存儲學(xué)生對象的集合,存儲3個(gè)學(xué)對象,使用程序?qū)崿F(xiàn)在控制臺遍歷該集合
思路:
1、定義學(xué)生類
2、創(chuàng)建集合對象
3、創(chuàng)建學(xué)生對象
4、添加學(xué)生對象到集合中
5、遍歷集合,采用通用遍歷格式實(shí)現(xiàn)
代碼實(shí)現(xiàn):
public class ArrayListTest02 {
public static void main(String[] args) {
//創(chuàng)建集合對象
ArrayList<Student> array = new ArrayList<>();
//創(chuàng)建學(xué)生對象
Student s1 = new Student("張三", 30);
Student s2 = new Student("李四", 33);
Student s3 = new Student("王五", 28);
//添加學(xué)生對象到集合中
array.add(s1);
array.add(s2);
array.add(s3);
//遍歷集合,采用通用遍歷格式實(shí)現(xiàn)
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
}
五、ArrayList存儲學(xué)生對象并遍歷升級版
需求:創(chuàng)建一個(gè)存儲學(xué)生對象的集合,存儲3個(gè)學(xué)生對象,使用程序?qū)崿F(xiàn)在控制臺遍歷該集合,學(xué)生的姓名和年齡來自于鍵盤錄入
思路:
1、定義學(xué)生類,為了鍵盤錄入數(shù)據(jù)方便,把學(xué)生類中的成員變量都定義為String類型
2、創(chuàng)建集合對象
3、鍵盤錄入學(xué)生對象所需要的數(shù)據(jù)
4、創(chuàng)建學(xué)生對象,把鍵盤錄入的數(shù)據(jù)賦值給學(xué)生對象的成員變量
5、往集合中添加學(xué)生對象
6、遍歷集合,采用通用遍歷格式實(shí)現(xiàn)
代碼實(shí)現(xiàn):
public class ArrayListTest {
public static void main(String[] args) {
//創(chuàng)建集合對象
ArrayList<Student> array = new ArrayList<Student>();
//為了提高代碼的復(fù)用性,我們用方法來改進(jìn)程序
addStudent(array);
addStudent(array);
addStudent(array);
//遍歷集合,采用通用遍歷格式實(shí)現(xiàn)
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
}
/*
兩個(gè)明確:
返回值類型:void
參數(shù):ArrayList<Student> array
*/
public static void addStudent(ArrayList<Student> array) {
//鍵盤錄入學(xué)生對象所需要的數(shù)據(jù)
Scanner sc = new Scanner(System.in);
System.out.println("請輸入學(xué)生姓名:");
String name = sc.nextLine();
System.out.println("請輸入學(xué)生年齡:");
String age = sc.nextLine();
//創(chuàng)建學(xué)生對象,把鍵盤錄入的數(shù)據(jù)賦值給學(xué)生對象的成員變量
Student s = new Student();
s.setName(name);
s.setAge(age);
//往集合中添加學(xué)生對象
array.add(s);
}
}到此這篇關(guān)于Java基礎(chǔ)學(xué)習(xí)之ArrayList類概述與常用方法的文章就介紹到這了,更多相關(guān)Java ArrayList類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Map實(shí)現(xiàn)線程安全的3種方式
本文主要介紹了Java中Map實(shí)現(xiàn)線程安全的3種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Spring實(shí)戰(zhàn)之Bean的后處理器操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean的后處理器操作,結(jié)合實(shí)例形式詳細(xì)分析了Bean的后處理器相關(guān)配置、操作方法及使用注意事項(xiàng),需要的朋友可以參考下2019-12-12
java的springboot實(shí)現(xiàn)將base64編碼轉(zhuǎn)換pdf
在Spring Boot中,將Base64編碼的字符串轉(zhuǎn)換為PDF文件并導(dǎo)出到客戶端,通常涉及幾個(gè)步驟:首先將Base64字符串解碼為字節(jié)數(shù)組,然后使用這些字節(jié)數(shù)據(jù)來創(chuàng)建PDF文件,并最終通過HTTP響應(yīng)將其發(fā)送給客戶端2024-08-08
簡述Java中進(jìn)程與線程的關(guān)系_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
在 Java 語言中,對進(jìn)程和線程的封裝,分別提供了 Process 和 Thread 相關(guān)的一些類。本文首先簡單的介紹如何使用這些類來創(chuàng)建進(jìn)程和線程2017-05-05
JAVA中ListIterator和Iterator詳解與辨析(推薦)
這篇文章主要介紹了JAVA中ListIterator和Iterator詳解與辨析,需要的朋友可以參考下2017-04-04
springboot2.x解決運(yùn)行順序及Bean對象注入順序的問題
這篇文章主要介紹了springboot2.x解決運(yùn)行順序及Bean對象注入順序的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

