コレクションコレクションの概要
- Java配列の長さは、固定されているプログラムを容易に記憶することができ、データのセットが固定されていない操作の数は、JDKのクラスライブラリは、Javaのセットを提供します
- 基本データ型のセットを記憶することができず、唯一のオブジェクトへの参照を格納することができる配列とは異なり。
- 要素の同じデータタイプを格納することができる配列要素の異なるタイプのコレクションを格納することができます
はじめにコレクション・フレームワーク
共通機能のコレクションセット
インタフェースjava.util.Collection
-
インターフェイスのすべてのトップレベルの単一のセットは、すべてのための一般的な方法の単一のセット内で定義され
-
任意の方法は、インタフェースコレクションの単一のセットを使用することができます
インタフェースCollectionの一般的な方法
-
パブリックブールの追加(Eのe)の現在のコレクションに指定されたオブジェクト。
-
公共のボイドをクリア()クリアし、すべての要素のセット。
-
パブリックブール削除(E e)の現在のアルバムに加えて、指定されたオブジェクトのセット。
-
パブリックブール包含の現在のセットがオブジェクトを指定されたかどうかを決定する(E、E)を含有します。
-
パブリックブールのisEmpty()は、現在のセットが空であるか否かを判定する。
-
公共int型のサイズは、()セット内の要素の数を返します。
-
パブリックオブジェクト[]のtoArray()配列に格納された要素の集合。
()メソッドを追加
役割:現在のコレクションに指定されたオブジェクト。
import java.util.Collection; import java.util.ArrayList; public class DemoCollectionAdd { public static void main(String[] args) { // 使用多态,创建一个ArrayList对象 Collection<String> collection = new ArrayList<>(); // 输出该集合的内容是为空的(其中它重写了toString方法) System.out.println("没有进行任何操作的ArrayList集合对象:" + collection); // 往ArrayList集合中添加元素,返回值只一个boolean值,一般不用接收这个返回值 boolean addReturn = collection.add("LeeHua"); System.out.println("往集合中添加一个元素后的返回值:" + addReturn); System.out.println("使用add方法往集合里面添加了元素后:" + collection); } }
输出结果: 没有进行任何操作的ArrayList集合对象:[] 往集合中添加一个元素后的返回值:true 使用add方法往集合里面添加了元素后:[LeeHua]
remove方法
作用:把给定的对象在当前集合中册除。
import java.util.Collection; import java.util.ArrayList; public class DemoCollectionRemove { public static void main(String[] args) { // 使用多态,创建一个ArrayList对象 Collection<String> collection = new ArrayList<>(); System.out.println("往集合中添加元素前的集合是:" + collection); // 往集合中添加元素 collection.add("一号"); collection.add("二号"); collection.add("三号"); collection.add("四号"); System.out.println("往集合中添加元素后的集合是:" + collection); // 使用remove方法,把给定的对象在当前集合中册除 // 如果要删除的元素存在该集合,那么就返回true // 否则返回false boolean removeReturn1 = collection.remove("一号"); System.out.println("删除元素\"一号\"的返回值:" + removeReturn1); System.out.println("删除元素\"一号\"后的集合是:" + collection); boolean removeReturn2 = collection.remove("十号"); System.out.println("删除元素\"十号\"的返回值:" + removeReturn2); System.out.println("删除元素\"十号\"后的集合是:" + collection); } }
输出结果: 往集合中添加元素前的集合是:[] 往集合中添加元素后的集合是:[一号, 二号, 三号, 四号] 删除元素"一号"的返回值:true 删除元素"一号"后的集合是:[二号, 三号, 四号] 删除元素"十号"的返回值:false 删除元素"十号"后的集合是:[二号, 三号, 四号]
contains方法
作用:判断当前集合中是否包合给定的对象。
import java.util.ArrayList; import java.util.Collection; public class DemoCollectionContains { public static void main(String[] args) { Collection<String> collection = new ArrayList<>(); // 往集合中添加元素 collection.add("对象1"); collection.add("对象2"); collection.add("对象3"); collection.add("对象4"); System.out.println("集合:" + collection); // 使用contains方法,判断当前集合中是否包合给定的对象 // 如果包合给定的对象,那么就返回true // 否则返回false boolean containsReturn1 = collection.constains("对象100"); System.out.println("是否包含\"对象100\":" + containsReturn1); boolean containsReturn2 = collection.constains("对象1"); System.out.println("是否包含\"对象1\":" + containsReturn2); } }
集合:[对象1, 对象2, 对象3, 对象4] 是否包含"对象100":false 是否包含"对象1":true
isEmpty方法
作用:判断当前集合是否为空。
import java.util.ArrayList; import java.util.Collection; public class DemoCollectionIsEmpty { public static void main(String[] args) { // 使用多态,创建一个ArrayList对象 Collection<String> collection = new ArrayList<>(); // 判断集合是否为空 boolean isEmptyReturn1 = collection.isEmpty(); System.out.println("集合是否为空:" + isEmptyReturn1); // 向集合里面添加元素 collection.add("一号元素"); // 判断集合是否为空 boolean isEmptyReturn2 = collection.isEmpty(); System.out.println("集合是否为空:" + isEmptyReturn2); } }
输出结果: 集合是否为空:true 集合是否为空:false
size方法
作用:返回集合中元素的个数。
import java.util.ArrayList; import java.util.Collection; public class DemoCollectionSize { public static void main(String[] args) { // 使用多态,创建一个ArrayList对象 Collection<String> collection = new ArrayList<>(); // 使用size方法,查看集合中的元素个数 int collectionSize1 = collection.size(); System.out.println("collectionSize1 = " + collectionSize1); // 往集合中添加元素 collection.add("一号元素"); collection.add("二号元素"); collection.add("三号元素"); collection.add("四号元素"); collection.add("五号元素"); // 使用size方法,再次查看集合中的元素个数 int collectionSize2 = collection.size(); System.out.println("collectionSize2 = " + collectionSize2); } }
输出结果: collectionSize1 = 0 collectionSize2 = 5
toArray方法
作用:把集合中的元素,存储到数组中。
import java.util.ArrayList; import java.util.Collection; /** * @Author: YiHua Lee * @Version: 1.8.0_201 Java SE 8 * @Application: IntelliJ IDEA * @CreateTime: 2020/1/12 14:08 * @Description: */ public class DemoCollectionToArray { public static void main(String[] args) { // 使用多态,创建一个ArrayList对象 Collection<String> collection = new ArrayList<>(); // 往集合中添加元素 collection.add("一号元素"); collection.add("二号元素"); collection.add("三号元素"); collection.add("四号元素"); collection.add("五号元素"); // 使用toArray方法,把集合中的元素,存储到数组中。 Object[] collectionToArray = collection.toArray(); // 遍历输出 for (int i = 0; i < collectionToArray.length; i++) { System.out.println(collectionToArray[i]); } } }
输出结果:
一号元素
二号元素
三号元素
四号元素
五号元素
clear方法
作用:清空集合中的所用元素
import java.util.ArrayList; import java.util.Collection; public class DemoCollectionClear { public static void main(String[] args) { // 使用多态,创建一个ArrayList对象 Collection<String> collection = new ArrayList<>(); // 往集合中添加元素 collection.add("一号元素"); collection.add("二号元素"); collection.add("三号元素"); collection.add("四号元素"); collection.add("五号元素"); System.out.println("清空集合元素之前:" + collection); // 使用clear方法,清空集合中的所用元素 collection.clear(); System.out.println("清空集合元素之后:" + collection); } }
输出结果:
清空集合元素之前:[一号元素, 二号元素, 三号元素, 四号元素, 五号元素]
清空集合元素之后:[]