Java集合的定義與Collection類使用詳解
什么是集合?
概念:對(duì)象的容器,定義了對(duì)多個(gè)對(duì)象進(jìn)行操作的常用方法??蓪?shí)現(xiàn)數(shù)組的功能。
集合和數(shù)組的區(qū)別:
- 數(shù)組長(zhǎng)度固定,集合長(zhǎng)度不固定
- 數(shù)組可以存儲(chǔ)基本類型和引用類型,集合只能引用類型
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("元素個(gè)數(shù)為:"+collection.size());
// 2.刪除元素
collection.remove("榴蓮");
System.out.println(collection);
System.out.println("元素個(gè)數(shù)為:"+collection.size());
// 3.遍歷元素
// 3.1增強(qiáng)for循環(huán)
System.out.println("-------------3.1增強(qiáng)for循環(huán)----------------");
for (Object object:collection) {
System.out.println(object);
}
System.out.println("-------------3.2使用迭代器Iterator----------------");
// 3.2使用迭代器Iterator,本身是一個(gè)接口
// 三種方法:hasNext()判斷是否有元素,next()獲取下一個(gè)元素,remove()刪除元素
Iterator it = collection.iterator();
while (it.hasNext()){
String s = (String)it.next();
System.out.println(s);
//it.remove();
}
System.out.println("元素個(gè)數(shù)為"+collection.size());
// 4.判斷:contains
System.out.println(collection.contains("西瓜"));
// 判斷是否為空
System.out.println(collection.isEmpty());
}
}注意:使用Collection是不能實(shí)例化的,但是可以通過(guò)new一個(gè)它的子類來(lái)創(chuàng)建對(duì)象的。

還有就是重點(diǎn)記住遍歷元素的方法。 迭代器Iterator。
迭代器Iterator:
三種方法hasNext()、next() 還有一個(gè)remove()用于刪除迭代器中的元素(在迭代器中,是不可以用collection.remove來(lái)刪除元素的)
原理:
先用hasNext()判斷是否有元素,如果有就下一個(gè)next(),依次類推。
使用Collection保存學(xué)生信息:
直接看代碼:
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("元素個(gè)數(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);
}
}
}運(yùn)行結(jié)果:

到此這篇關(guān)于Java集合的定義與Collection類使用詳解的文章就介紹到這了,更多相關(guān)Java集合與Collection內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中樂(lè)觀鎖與悲觀鎖區(qū)別及使用場(chǎng)景分析
本文主要介紹了java中樂(lè)觀鎖與悲觀鎖區(qū)別及使用場(chǎng)景分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
rocketmq的AclClientRPCHook權(quán)限控制使用技巧示例詳解
這篇文章主要為大家介紹了rocketmq的AclClientRPCHook使用技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
springcloud gateway聚合swagger2的方法示例
這篇文章主要介紹了springcloud gateway聚合swagger2的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
springboot?log4j2.xml如何讀取application.yml中屬性值
這篇文章主要介紹了springboot?log4j2.xml如何讀取application.yml中屬性值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
MyBatis注解實(shí)現(xiàn)動(dòng)態(tài)SQL問(wèn)題
這篇文章主要介紹了MyBatis注解實(shí)現(xiàn)動(dòng)態(tài)SQL問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
AsyncHttpClient exception異常源碼流程解析
這篇文章主要為大家介紹了AsyncHttpClient的exception源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
SpringAOP切入點(diǎn)規(guī)范及獲取方法參數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了SpringAOP切入點(diǎn)規(guī)范及獲取方法參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java.net.SocketException: Connection reset 解決方法
最近糾結(jié)致死的一個(gè)java報(bào)錯(cuò)java.net.SocketException: Connection reset 終于得到解決2013-03-03
Java POI讀取excel中數(shù)值精度損失問(wèn)題解決
這篇文章主要介紹了Java POI讀取excel中數(shù)值精度損失問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

