想了解更多请查看java学习(idea版)
目录
Collection集合
集合:集合是java中提供的一种容器,可以用来存储多个数据
集合和数组既然都是容器,它们有啥区别呢?
- 数组的长度是固定的。集合的长度是可变的。
- 数组中存储的是同一类型的元素,可以存储基本数据类型值。集合存储的都是对象。而且对象的类型可以不一致。在开发中一般当对象多的时候,使用集合进行存储。
集合按照其存储结构可以分为两大类,分别是
单列集合 java.util.Collection
和
双列集合 java.util.Map,
今天
我们主要学习
Collection
集合
集合框架
(重点)Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是java.util.List 和 java.util.Set 。其中, List 的特点是元素有序、元素可重复。 Set 的特点是元素无 序,而且不可重复。 List 接口的主要实现类有 java.util.ArrayList 和 java.util.LinkedList , Set 接口 的主要实现类有 java.util.HashSet 和 java.util.TreeSet 。
Collection 常用功能
public boolean add(E e) : 把给定的对象添加到当前集合中 。
public void clear() :清空集合中所有的元素。
public boolean remove(E e) : 把给定的对象在当前集合中删除。
public boolean contains(E e) : 判断当前集合中是否包含给定的对象。
public boolean isEmpty() : 判断当前集合是否为空。
public int size() : 返回集合中元素的个数。
public Object[] toArray() : 把集合中的元素,存储到数组中。
列:接口共性方法练习
public static void main(String[] args) {
//创建集合对象
Collection<String> coll = new ArrayList<>();
System.out.println(coll);//重写了toString方法 []
//1 添加数据 public boolean add(E e):
boolean a = coll.add("老赵");
coll.add("老钱");
coll.add("老孙");
coll.add("老孙");
System.out.println(coll);//[老赵, 老钱, 老孙, 老孙]
//2 删除数据 public boolean remove(E e): 把给定的对象在当前集合中删除。
boolean b = coll.remove("老孙");
System.out.println(coll);//[老赵, 老钱, 老孙]
//3 判断对象在集合中是否存在
boolean c = coll.contains("老孙");
System.out.println(c);
//4 判断集合是否为空 public boolean isEmpty(): 判断当前集合是否为空。
boolean d = coll.isEmpty();
System.out.println(d);
//5 判断集合的长度 public int size(): 返回集合中元素的个数。
int size = coll.size();
System.out.println(size);//3
//6 public Object[] toArray(): 把集合中的元素,存储到数组中。
Object[] arr = coll.toArray();
System.out.println(arr);//[Ljava.lang.Object;@50cbc42f
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
//7 清空结合 public void clear() :清空集合中所有的元素。但是不删除集合,集合还存在
coll.clear();
System.out.println("\n集合为:"+coll);
System.out.println("集合是否为空:"+coll.isEmpty());
}
Iterator迭代器
Iterator迭代器引入
在程序开发中,经常需要遍历集合中的所有元素。针对这种需求,JDK专门提供了一个接口 java.util.Iterator 。 Iterator 接口也是Java集合中的一员,但它与 Collection 、 Map 接口有所不同, Collection 接口与 Map 接口主要用于存储元素,而 Iterator 主要用于迭代访问(即遍历) Collection 中的元 素,因此 Iterator 对象也被称为迭代器。
- public Iterator iterator() 获取迭代器的方法(创建Iterator对象)
- public E next() :返回迭代的下一个元素。
- public boolean hasNext() :如果仍有元素可以迭代,则返回 true。
Iterator原理
通过调用t集合的iterator()方法获得 迭代器对象,然后使用hashNext()方法判断集合中是否存在下一个元素,如果存在,则调用next()方法将元素取 出,否则说明已到达了集合末尾,停止遍历元素
列:Iterator练习
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01Iterator {
public static void main(String[] args) {
//创建集合
Collection<String> coll=new ArrayList<>();
coll.add("老赵");
coll.add("老钱");
coll.add("老孙");
coll.add("老李");
Iterator<String> it = coll.iterator();//使用集合中的方法iterator()获取迭代器的实现类对象(多态写法)
//a 使用while循环对迭代器遍历
while (it.hasNext()){
String e = it.next();
System.out.println(e);
}
}
}
for...each语句
Collection<Integer> a=new ArrayList<>();
a.add(1);
a.add(2);
a.add(3);
a.add(4);
for (Integer item : a) {
System.out.println(item);
}
//数组循环遍历
String[] str=new String[]{"hello","world","java","welcome"};
for (String s : str) {
System.out.println(s);
}