一、ArrayList
集合和數組的區別 :
共同點:都是存儲數據的容器
不同點:數組的容量是固定的,集合的容量是可變的
1.ArrayList的構造方法和添加方法
ArrayList :
可調整大小的數組實現
: 是一種特殊的數據類型,泛型。
怎么用呢 ?
在出現E的地方我們使用引用數據類型替換即可
舉例:ArrayList, ArrayList
2.ArrayList類常用方法【應用】
成員方法 :
示例代碼 :
public class ArrayListDemo02 { public static void main(String[] args) { //創建集合 ArrayList array = new ArrayList(); //添加元素 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)); //System.out.println(array.get(3)); //?????? 自己測試 //public int size():返回集合中的元素的個數 System.out.println(array.size()); //輸出集合 System.out.println("array:" + array); } }
3.ArrayList存儲字符串并遍歷
案例需求 :
創建一個存儲字符串的集合,存儲3個字符串元素,使用程序實現在控制臺遍歷該集合
實現步驟 :
1:創建集合對象 2:往集合中添加字符串對象 3:遍歷集合,首先要能夠獲取到集合中的每一個元素,這個通過get(int index)方法實現 4:遍歷集合,其次要能夠獲取到集合的長度,這個通過size()方法實現 5:遍歷集合的通用格式
代碼實現 :
/* 思路: 1:創建集合對象 2:往集合中添加字符串對象 3:遍歷集合,首先要能夠獲取到集合中的每一個元素,這個通過get(int index)方法實現 4:遍歷集合,其次要能夠獲取到集合的長度,這個通過size()方法實現 5:遍歷集合的通用格式 */ public class ArrayListTest01 { public static void main(String[] args) { //創建集合對象 ArrayList array = new ArrayList(); //往集合中添加字符串對象 array.add("劉正風"); array.add("左冷禪"); array.add("風清揚"); //遍歷集合,其次要能夠獲取到集合的長度,這個通過size()方法實現 // System.out.println(array.size()); //遍歷集合的通用格式 for(int i=0; i4.ArrayList存儲學生對象并遍歷
案例需求 :
創建一個存儲學生對象的集合,存儲3個學生對象,使用程序實現在控制臺遍歷該集合
實現步驟 :

1:定義學生類
2:創建集合對象
3:創建學生對象
4:添加學生對象到集合中
5:遍歷集合,采用通用遍歷格式實現
代碼實現 :
/* 思路: 1:定義學生類 2:創建集合對象 3:創建學生對象 4:添加學生對象到集合中 5:遍歷集合,采用通用遍歷格式實現 */ public class ArrayListTest02 { public static void main(String[] args) { //創建集合對象 ArrayList array = new ArrayList<>(); //創建學生對象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("風清揚", 33); Student s3 = new Student("張曼玉", 18); //添加學生對象到集合中 array.add(s1); array.add(s2); array.add(s3); //遍歷集合,采用通用遍歷格式實現 for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "," + s.getAge()); } } }
5.鍵盤錄入學生信息到集合
案例需求 :
創建一個存儲學生對象的集合,存儲3個學生對象,使用程序實現在控制臺遍歷該集合
學生的姓名和年齡來自于鍵盤錄入
實現步驟 :
1:定義學生類,為了鍵盤錄入數據方便,把學生類中的成員變量都定義為String類型
2:創建集合對象
3:鍵盤錄入學生對象所需要的數據
4:創建學生對象,把鍵盤錄入的數據賦值給學生對象的成員變量
5:往集合中添加學生對象
6:遍歷集合,采用通用遍歷格式實現
代碼實現 :
/* 思路: 1:定義學生類,為了鍵盤錄入數據方便,把學生類中的成員變量都定義為String類型 2:創建集合對象 3:鍵盤錄入學生對象所需要的數據 4:創建學生對象,把鍵盤錄入的數據賦值給學生對象的成員變量 5:往集合中添加學生對象 6:遍歷集合,采用通用遍歷格式實現 */ public class ArrayListTest { public static void main(String[] args) { //創建集合對象 ArrayList array = new ArrayList(); //為了提高代碼的復用性,我們用方法來改進程序 addStudent(array); addStudent(array); addStudent(array); //遍歷集合,采用通用遍歷格式實現 for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "," + s.getAge()); } } /* 兩個明確: 返回值類型:void 參數:ArrayList array */ public static void addStudent(ArrayList array) { //鍵盤錄入學生對象所需要的數據 Scanner sc = new Scanner(System.in); System.out.println("請輸入學生姓名:"); String name = sc.nextLine(); System.out.println("請輸入學生年齡:"); String age = sc.nextLine(); //創建學生對象,把鍵盤錄入的數據賦值給學生對象的成員變量 Student s = new Student(); s.setName(name); s.setAge(age); //往集合中添加學生對象 array.add(s); } }
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。