集合框架介绍:
-
java.util.Collection接口
所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
任意的单列集合都可以使用Collection接口中的方法。
共性的方法:
public boolean add(E e):把给定的对象添加到当前集合中。
public void clear():清空集合中所有的元素。
public boolean remove(E e):把给定的对象在当前集合中删除。
public boolean contain(E e):判断当前集合中是否包含给定的对象。
public boolean isEmpty():判断当前集合是否维空。
public int size():返回集合中元素的个数。
public Object[] toArray():把集合中的元素,存储到数组中。
import java.util.Collection;
import java.util.ArrayList;
public class Demo01Collection{
public static void main(String[] args){
//创建一个集合对象,可以使用多态。
Collection<String> coll = new ArrayList<>();
System.out.println(coll);//重写了toString方法
/*
public boolean add(E e):把给定的对象添加到当前集合中。
返回值是一个boolean值,一般都返回true,所以可以不用接收
*/
boolean b1 = coll.add("a");
System.out.println(b1);//true
System.out.println(coll);
coll.add("b");
coll.add("c");
coll.add("d");
coll.add("e");
System.out.println(coll);
/*
public boolean remove(E e):把给定的对象在当前集合中删除。
返回值是一个boolean值,集合中存在元素,删除元素,返回true.
集合中不存在元素,删除失败,返回false.
*/
boolean b2 = coll.remove("c");
System.out.println(b2);
System.out.println(coll);
boolean b3 = coll.remove("f");
System.out.println(b3);
/*
public boolean contains(E e):判断当前集合中是否包含给定的对象。
包含则返回true,否则返回false.
*/
boolean b4 = coll.contains("e");
System.out.println(b4);
//public boolean isEmpty():判断当前集合是否为空。空则返回true,否则返回false.
boolean b5 = coll.isEmpty();
System.out.println(b5);
//public int size():返回集合中元素的个数。
int size = coll.size();
System.out.println(size);
//public Object[] toArray():把集合中的元素,存储到数组中。
Object[] arr = coll.toArray();
for(int i = 0;i<arr.length;i++){
System.out.println(arr[i]);
}
//public void clear():清空集合中所有的元素,但是不删除集合,集合还存在。
coll.clear();
System.out.println(coll);
System.out.println(coll.isEmpty());
}
}
2.Iterator 接口
java.util.Iterator接口:迭代器(对集合进行遍历)
有两个常用的方法:
boolean hasNext()如果仍有元素可以迭代,则返回true.
判断集合中还有没有下一个元素,有就返回true,没有就返回false.
E next()返回迭代的下一个元素。
取出集合中的下一个元素。
Iterator迭代器,是一个接口,我们无法直接使用,需要使用Iterator接口的实现类对象,获取实现类的方式比较特殊。
Collection接口中有一个方法,叫Iterator(),这个方法返回的就是迭代器的实现类对象。
迭代器的使用步骤:
- 使用集合中的方法Iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
- 使用Iterator接口中的方法hasNext判断还有没有下一个元素。
使用Iterator接口中的方法next取出集合中的下一个元素。
/*
1.使用集合中方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
注意:
Iterator<E>接口也是有泛型的,迭代器的泛型跟着集合邹,集合是什么泛型,迭代器就是什么泛型
*/
import java.util.Iterator;
import java.util.Collection;
import java.util.ArrayList;
public class Demo01Iterator{
public static void main(String[] args){
Collection<String> coll = new ArrayList<>();
coll.add("a");
coll.add("b");
coll.add("c");
coll.add("d");
//多态 接口 实现类对象
Iterator<String> it = coll.iterator();
//使用Iterator接口中的方法hasNext判断还有没有下一个元素。
boolean b = it.hasNext();
System.out.println(b);
while(it.hasNext()){
String s1 = it.next();
System.out.println(s1);
}
}
}
3.增强for循环
增强for循环:底层使用的也是迭代器,使用for循环的格式,简化了迭代器的书写。
Collection<E> extends Iterator<E>:所有的单列集合都可以使用增强for
public interface Interable<T>实现这个接口允许对象成为“foreach”语句的目标。
增强for循环:用来遍历集合和数组
格式:
for(集合/数组的数据类型 变量名 :集合名/数组名){
sout(变量名);
}
public class Demo01Foreach{
public static void main(String[] args){
int[] arr = {1,2,3,4};
for(int i : arr){
System.out.println(i);
}
}
}