失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > -06-19复习java Collection集合 Iterator接口_迭代器 增强for循环 泛型

-06-19复习java Collection集合 Iterator接口_迭代器 增强for循环 泛型

时间:2023-08-11 12:07:56

相关推荐

-06-19复习java Collection集合 Iterator接口_迭代器 增强for循环 泛型

-06-19复习java

Collection集合Iterator接口_迭代器增强for循环泛型

Collection集合

java.util.coLlection接口

所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法,如图04-10-1,任意的单列集合都可以使用collection接口中的方法

共性的方法:

public boolean add(E e):把给定的对象添加到当前集合中。public void clearo:清空集合中所有的元素。public boolean remove(E e):把给定的对象在当前集合中删除。Tpublic boolean contains(E e):判断当前集合中是否包含给定的对象。public boolean isEmpty():判断当前集合是否为空。public int size():返回集合中元素的个数。public object[] toArray():把集合中的元素,存储到数组中。

add方法:

collection<String> col1. = new ArrayList<>();system.out.println(col1);//重写了toString方法/*public boolean add(E e):把给定的对象添加到当前集合中。返回值是一个boolean值,一般都返回true,所以可以不用接牧*/boolean b1 = coll.add(“张三“);system.out.println("b1 :"+b1);//b1;truesystem.out.println(coll);//[张三]col1.add("李四");coll.add(”赵六");coll.add("田七");system.out.print1n(coll);//[张三,李四,赵六,田女]

remove方法:

/*public boolean remove(E e):把给定的对象在当前集合中删除。返回值是一个boolean值,集合中存在元素,删除元素,返回true集合中不存在元素,删除失败,返回false*/boolean b2 = coll.remove("赵六");system.out.println("b2:"+b2);//b2:true

contains方法:

/*public boolean contains(E e):判断当前集合中是否包含给定的对象。包含返回true不包含返回false*/boolean b4 = coll.contains(“李四");system.out.print1n( "b4 : "+b4);//b4:trueboolean b5 = col1.contains("赵四");system.out.println( "b5: "+b5);//b5 ;false

isEmpty()方法:

///public boolean isEmpty( ):判断当前集合是否为空。集合为空返回true ,集合不为空返回falseboolean b6 = col1.isEmpty();system.out.println("b6:"+b6) ;//b6:false

size、toArray、clear方法:

// public int size():返回集合中元素的个数。int size = coll.size();system.out.println( "size: "+size);//size: 3// public 0bject[] toArray():把集合中的元素,存储到数组中。0bject[] arr = coll.toArray();for (int i = e; i < arr.length; i++){system.out.println(arr[i]);}//public void clear():清空集合中所有的元素。但是不删除集合,集合还存在coll.clear();System.out.println(coll);//[]:此时集合已经被清空,没有任何元素

注意:以上这些方法都是共性的,以下这些集合都可以使用上面的方法

Vector集合、ArrayList集合、LinkedList集合、TreeSet集合、HashSet集合、LinkedHashSet集合

Iterator接口_迭代器

java.util.Iterator接口:迭代器(对集合进行遍历)

有两个常用的方法:

boolean hasNext() 如果仍有元素可以迭代,则返回true。

判断集合中还有没有下一个元素,有就返回true,没有就返回falseE next()返回迭代的下一个元素。取出集合中的下一个元素Iterator迭代器,是一个接口,我们无法直接使用,需要使用Iterator接口的实现类对象,获取实现类的方式比较特殊collection接口中有一个方法,叫iterator(),这个方法返回的就是迭代器的实现类对象Iterator iterator()返回在此collection 的元素上进行迭代的迭代器。

迭代器的使用步骤(重点):

1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)2.使用Iterator接口中的方法hasNext判断还有没有下一个元素3.使用Iterator接口中的方法next取出集合中的下一个元素

public static void main(string[] args) {//创建一个集合对象collection<string> coll = new ArrayList<>();//往集合中添加元素coll.add("姚明");coll.add("科比");col1.add("麦迪");coll.add("詹姆斯");coll.add("艾弗森");/*1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)注意:Iterator<E>接口也是有泛型的,迭代器的泛型跟着集合走,集合是什么泛型,迭代器就是什么泛型*///多态接口实现类对象Iterator<string> it = coll.iterator();/*发现使用迭代器取出集合中元素的代码,是一个重复的过程所以我们可以使用循环优化不知道集合中有多少元素,使用whiLe循环循环结束的条件, hasNext方法返回false */while(it.hasNext()){string e = it.nextsystem.out.println(e);}

还可以使用for循环遍历,不过常用while循环

for( Iterator<String> it2 = coll.iterator();it2.hasNext();){string e = it2.next();system.out.println(e) ;}

增强for循环

增强for循环:底层使用的也是迭代器,使用for循环的格式,简化了迭代器的书写是JDK1.5之后出现的新特性collection extends Iterable:所有的单列集合都可以使用增强forpublic interface Iterable实现这个接口允许对象成为“foreach”语句的目标。增强for循环:用来遍历集合和数组

格式:

for(集合/数组的数据类型 变量名:集合名/数组名){ISystem.out.println(变量名);}

演示增强for循环遍历(针对于数组、集合):

public static void main(String[]args){collection1();collection2();}//增强for循环遍历数组private static void collection1(){int[] arr = {1,2,3,4,5,6}for(int i :arr){System.out.println(i)}}//增强for循环遍历集合private static void collection2(){ArrayList<String> list = new ArrayList<>;list.add("aaa")list.add("bbb")for(String li:list){System.out.println(li);}}

泛型

使用泛型的好处就是可以把程序的报错从运行期的异常提升至编译期就会提示异常。

定义一个含有泛型的类,模拟Arraylist集合泛型是一个未知的数据类型,当我们不确定什么什么数据类型的时候,可以使用泛型泛型可以接收任意的数据类型,可以使用Integer,String

, student. . .创建对象的时候确定泛型的数据类型

//E:表示泛型public class Genericclass<E>{private E name;public E getName() {return name;}public void setName(E name) {this.name = name;}}

定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间

格式:修饰符<泛型>返回值类型方法名(参数列表(使用泛型)){方法体;}

含有泛型的方法,在调用方法的时候确定泛型的数据类型传递什么类型的参数,泛型就是什么类型

public class GenericMethod {//定义一个含有泛型的方法public <M> void methode1(M m){system.out.println(m);}//定义一个含有泛型的静态方法public static <s> void method02(S s){system.out.println(s);}

如果觉得《-06-19复习java Collection集合 Iterator接口_迭代器 增强for循环 泛型》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。