Java集合的定義與Collection類使用詳解
什么是集合?
概念:對象的容器,定義了對多個對象進行操作的常用方法。可實現(xiàn)數(shù)組的功能。
集合和數(shù)組的區(qū)別:
- 數(shù)組長度固定,集合長度不固定
- 數(shù)組可以存儲基本類型和引用類型,集合只能引用類型
Collection :
Collection體系結(jié)構(gòu):
Collection的使用:包括增加元素、刪除元素、遍歷元素(兩種方法)和判斷
直接看代碼:
package com.collections; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Demo01 { public static void main(String[] args) { // 創(chuàng)建集合 Collection collection = new ArrayList(); // 1.添加元素 collection.add("蘋果"); collection.add("梨子"); collection.add("榴蓮"); System.out.println(collection); System.out.println("元素個數(shù)為:"+collection.size()); // 2.刪除元素 collection.remove("榴蓮"); System.out.println(collection); System.out.println("元素個數(shù)為:"+collection.size()); // 3.遍歷元素 // 3.1增強for循環(huán) System.out.println("-------------3.1增強for循環(huán)----------------"); for (Object object:collection) { System.out.println(object); } System.out.println("-------------3.2使用迭代器Iterator----------------"); // 3.2使用迭代器Iterator,本身是一個接口 // 三種方法:hasNext()判斷是否有元素,next()獲取下一個元素,remove()刪除元素 Iterator it = collection.iterator(); while (it.hasNext()){ String s = (String)it.next(); System.out.println(s); //it.remove(); } System.out.println("元素個數(shù)為"+collection.size()); // 4.判斷:contains System.out.println(collection.contains("西瓜")); // 判斷是否為空 System.out.println(collection.isEmpty()); } }
注意:使用Collection是不能實例化的,但是可以通過new一個它的子類來創(chuàng)建對象的。
還有就是重點記住遍歷元素的方法。 迭代器Iterator。
迭代器Iterator:
三種方法hasNext()、next() 還有一個remove()用于刪除迭代器中的元素(在迭代器中,是不可以用collection.remove來刪除元素的)
原理:
先用hasNext()判斷是否有元素,如果有就下一個next(),依次類推。
使用Collection保存學生信息:
直接看代碼:
Student類
package com.collections.test; public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
主方法:
package com.collections.test; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Demo01 { public static void main(String[] args) { Student s1 = new Student("aaa",18); Student s2 = new Student("bbb",19); Student s3 = new Student("ccc",20); Collection collection = new ArrayList(); collection.add(s1); collection.add(s2); collection.add(s3); System.out.println("元素個數(shù)為:"+collection.size()); System.out.println(collection.toString()); // collection.remove(s1); // collection.remove(new Student("ccc",20)); System.out.println("刪除后:"+collection.size()); System.out.println(collection.toString()); // 3.遍歷 for (Object object:collection) { Student s = (Student)object; System.out.println(s); } System.out.println("-----------------------------------------"); // 迭代器 Iterator it = collection.iterator(); while(it.hasNext()){ Student s = (Student) it.next(); System.out.println(s); } } }
運行結(jié)果:
到此這篇關(guān)于Java集合的定義與Collection類使用詳解的文章就介紹到這了,更多相關(guān)Java集合與Collection內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring mvc中@RequestBody注解的作用說明
這篇文章主要介紹了spring mvc中@RequestBody注解的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Java中的Set接口實現(xiàn)類HashSet和LinkedHashSet詳解
這篇文章主要介紹了Java中的Set接口實現(xiàn)類HashSet和LinkedHashSet詳解,Set接口和java.util.List接口一樣,同樣繼承自Collection接口,它與Collection接口中的方法基本一致,并沒有對Collection接口進行功能上的擴充,只是比Collection接口更加嚴格了,需要的朋友可以參考下2024-01-01解讀Spring接口方法加@Transactional失效的原因
這篇文章主要介紹了Spring接口方法加@Transactional失效的原因解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03