Collection集合常用功能,lterator接口介绍
Collection集合常用功能:
java.utiL.collection接口
所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
任意的单列集合都可以使用collection接口中的方法
Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可
用于操作所有的单列集合。方法如下∶
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 class danlie { public static void main(String[] args) { Collection<String> lists = new ArrayList<>(); //public booLean add(E e):把指定的对象添加到当前集合中。 boolean adds = lists.add("剑术"); System.out.println("adds:"+adds); System.out.println(lists); boolean adds1 = lists.add("冰皇"); System.out.println("adds1:"+adds1); System.out.println(lists); lists.add("焰枪"); lists.add("冰能"); System.out.println(lists); //public booLean remove(E e):把指定的对象在当前集合中删除。 boolean move = lists.remove("剑术"); System.out.println("move:"+move); System.out.println(lists); boolean move1 = lists.remove("剑碑"); System.out.println("move1:"+move1); System.out.println(lists); //public booLean contains(Ee):判断当前集合中是否包含指定的对象。 boolean context = lists.contains("冰皇"); System.out.println("context:"+context); System.out.println(lists); boolean context1 = lists.contains("火皇"); System.out.println("contains1:"+context1); System.out.println(lists); //public boolean isEmpty():判断当前集合是否为空。 boolean emptys = lists.isEmpty(); System.out.println(emptys); //public object[] toArray():把集合中的元素,存储到数组中。 Object[] object = lists.toArray(); for (int i = 0; i <object.length ; i++) { System.out.println(object[i]); } //public void clear():清空集合中所有的元素。但不删除集合 lists.clear(); System.out.println(lists); } }
public class jihe { public static void main(String[] args) { Collection<String> list = new ArrayList<>(); //public booLean add(E e):把指定的对象添加到当前集合中。 boolean add = list.add("阴阳"); System.out.println("adds:"+add); System.out.println(list); boolean add1 = list.add("天道"); System.out.println("add1:"+add1); System.out.println(list); list.add("法则"); list.add("铭文"); System.out.println(list); //public booLean remove(E e):把指定的对象在当前集合中删除。 boolean remove = list.remove("阴阳"); System.out.println("remove:"+remove); System.out.println(list); boolean remove1 = list.remove("黑白"); System.out.println("remove1:"+remove1); System.out.println(list); //public booLean contains(Ee):判断当前集合中是否包含指定的对象。 boolean contains = list.contains("冰皇"); System.out.println("contains:"+contains); System.out.println(list); boolean contains1 = list.contains("火皇"); System.out.println("contains1:"+contains1); System.out.println(list); //public boolean isEmpty():判断当前集合是否为空。 boolean empty = list.isEmpty(); System.out.println(empty); //public object[] toArray():把集合中的元素,存储到数组中。 Object[] objects = list.toArray(); for (int i = 0; i <objects.length ; i++) { System.out.println(objects[i]); } //public void clear():清空集合中所有的元素。但不删除集合 list.clear(); System.out.println(list); } }
public class list { public static void main(String[] args) { Collection<String> arrayList = new ArrayList<>(); //public booLean add(E e):把指定的对象添加到当前集合中。 boolean tdd = arrayList.add("星辰"); System.out.println("tdd:"+tdd); System.out.println(arrayList); boolean tdd1 = arrayList.add("遁天"); System.out.println("tdd1:"+tdd1); System.out.println(arrayList); arrayList.add("血影"); arrayList.add("雷刀"); System.out.println(arrayList); //public booLean remove(E e):把指定的对象在当前集合中删除。 boolean movesy = arrayList.remove("星辰"); System.out.println("movesy:"+movesy); System.out.println(arrayList); boolean movesy1 = arrayList.remove("恒星"); System.out.println("movesy1:"+movesy1); System.out.println(arrayList); //public booLean contains(Ee):判断当前集合中是否包含指定的对象。 boolean tains = arrayList.contains("遁天"); System.out.println("tains:"+tains); System.out.println(arrayList); boolean tains1 = arrayList.contains("天盾"); System.out.println("tains1:"+tains1); System.out.println(arrayList); //public boolean isEmpty():判断当前集合是否为空。 boolean emptyse = arrayList.isEmpty(); System.out.println(emptyse); //public object[] toArray():把集合中的元素,存储到数组中。 Object[] objectse = arrayList.toArray(); for (int i = 0; i <objectse.length ; i++) { System.out.println(objectse[i]); } //public void clear():清空集合中所有的元素。但不删除集合 arrayList.clear(); System.out.println(arrayList); } }
public class Arraylist { public static void main(String[] args) { Collection<String> arrayLists = new ArrayList<>(); //public booLean add(E e):把指定的对象添加到当前集合中。 boolean tdds = arrayLists.add("吞噬"); System.out.println("tdds:"+tdds); System.out.println(arrayLists); boolean tdds1 = arrayLists.add("摩云"); System.out.println("tdds1:"+tdds1); System.out.println(arrayLists); arrayLists.add("恶魔"); arrayLists.add("永恒"); System.out.println(arrayLists); //public booLean remove(E e):把指定的对象在当前集合中删除。 boolean removesy = arrayLists.remove("吞噬"); System.out.println("removesy:"+removesy); System.out.println(arrayLists); boolean removesy1 = arrayLists.remove("巨兽"); System.out.println("removesy1:"+removesy1); System.out.println(arrayLists); //public booLean contains(Ee):判断当前集合中是否包含指定的对象。 boolean containset = arrayLists.contains("摩云"); System.out.println("containset:"+containset); System.out.println(arrayLists); boolean containset1 = arrayLists.contains("长藤"); System.out.println("containset1:"+containset1); System.out.println(arrayLists); //public boolean isEmpty():判断当前集合是否为空。 boolean emptyset = arrayLists.isEmpty(); System.out.println(emptyset); //public object[] toArray():把集合中的元素,存储到数组中。 Object[] objectsey = arrayLists.toArray(); for (int i = 0; i <objectsey.length ; i++) { System.out.println(objectsey[i]); } //public void clear():清空集合中所有的元素。但不删除集合 arrayLists.clear(); System.out.println(arrayLists); } }
lterator接口介绍:
在程序开发中,经常需要遍历集合中的所有元素。针对这种需求,JDK专门提供了一个接口
java.util.Iterator。Iterator接口也是Java集合中的一员,但它与collection、Map接口有所不同,
collection接口与Map接口主要用于存储元素,而Iterator 主要用于迭代访问(即遍历) collection中的
元素,因此Iterator对象也被称为迭代器。
想要遍历Collection集合,那么就要获取该集合迭代器完成迭代操作,下面介绍一下获取迭代器的方法︰
public Iterator iterator():获取集合对应的迭代器,用来遍历集合中的元素的。
下面介绍一下迭代的概念∶
1.迭代:即Collection集合元素的通用获取方式。在取元素之前先要判断集合中有没有元素,如果有,就把这个
元素取出来,继续在判断,如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术
语称为迭代。
迭代器的使用步骤(重点):
1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
2.使用Iterator接口中的方法hasNext判断还有没有下一个元素
3.使用Iterator接口中的方法next取出集合中的下一个元素