JAVA高级应用之集合 迭代器
2018-01-18 20:24
387 查看
JAVA高级应用之集合
collection类
集合分类
collection基本方法
1.添加方法Collection colleation = new ArrayList(); collection.add();//add方法有返还值,永远返还true ArrayList什么时候会添加失败? ArrayList不可能添加失败 不能添加失败 为什么要设置返回值? 设计思想:不是只有ArrayList一个类实现collection,还有set类等其它类要实现这个借口, 而其他一些类在实现这个接口的时候需要返回值 boolean bool = collection.add("a"); //true boolean bool = collection.add(null); 当你向集合中添加基本数据类型的时候 系统会帮着进行自动装箱 把基本数据类型编程它的包装类
2.打印集合
System.out.println(collection);
3.获取集合的长度
System.out.println(collection.size());
4.判断是否包含这个元素
boolean bool = collection.contains("bbb"); 包含返回true 不包含返回false
5.从集合中删除一个元素
在原集合基础上进行操作 boolean bool1 = collection.remove("b"); 成功删除返回true 删除失败返回false
6.判断集合是否为空
boolean bool2 = collection.isEmpty(); 如果为空 返回true 不为空 返回false
添加 a b c d
Collection collection = new ArrayList(); colleation.add("a"); collection.add("b"); collection.add("c"); collection.add("d"); System.out.println(collection); Object[] array = collection.toArray(); for(int i = 0; i < array.length(); i++){ System.out.println(array[i]); }
代码示例
/* * 创建一个集合 * 保存三个学生 * 遍历打印学生姓名 */ class Student{ private String name; private int age; get函数 set函数 } class fun1{ Collection collection = new Collection(); collection.add(new Student("天启坦克",12)); collection.add(new Student("猛犸坦克",13)); collection.add(new Student("帝王坦克",14)); Object[] array = collection.toArray(); for(int i = 0; i < array.length; i++){ Student student = (Student)array[i]; System.out.println(student.getName()); } }
7.removeAll函数
Collection collection1 = new ArrayList(); Collection collection2 = new ArrayList(); collection1.add("a"); collection1.add("b"); collection1.add("c"); collection2.add("a"); collection2.add("b"); collection2.add("d"); boolean bool = collection1.removeAll(collection2); System.out.println(collection1); System.out.println(collection); System.out.println(bool); 从collection1中删除collection1与collection2的交集,如果成功删除了元素,返回true,如果没有 删除元素,则返回false // 这个方法 删除 将两个集合重合的元素删除 // 谁调用这个方法 就删除谁的元素 另外一个集合不变 // 只删除交集 重复也可以删
8.retainAll函数
Collection collection1 = new ArrayList(); Collection collection2 = new 4000 ArrayList(); collection1.add("a"); collection1.add("b"); collection1.add("c"); collection2.add("a"); collection2.add("b"); collection2.add("d"); boolean bool = colleation1.retainAll(colleation2); System.out.println(collection1); System.out.println(collection); System.out.println(bool); 在collection1中保留collection1与collection2交集的部分,collection2不变,如果collection 发生了变化返回true,没发生变化返回false // 把两个集合的交集取出来 保存在(谁调的方法 保存在谁那) // collection1集合和collection2集合 在求出交集 放到collection1中 // 如果collection1和原来的自己对比 没发生变化返回 false // 发生变化返回true
9.addAll
Collection array = new ArrayList(); array.add("aaa"); array.add("ddd"); array.add("cccc"); Collection arr = new ArrayList(); arr.add("ddd"); arr.add("cccc"); arr.add("ww"); boolean bool = array.addAll(arr); System.out.println(arr); System.out.println(array); System.out.println(bool); 将arr数组中的元素添加到array数组中去
Iterator迭代器
/* * 创建一个集合 * 保存3学生 * 使用迭代器遍历 打印学生姓名 */ Collection collection = new ArrayList(); collection.add(new Student("张三",18)); collection.add(new Student("李四", 19)); collection.add(new Student("王五",20)); while(iterator.hasNext()){ Student student = (Student)iterator.next(); System.out.println(student.getName()); }
相关文章推荐
- Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
- Java多线程与并发库高级应用-同步集合
- Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
- Java 回顾笔记_集合框架-泛型高级应用
- 黑马程序员_JAVA学习日记_JAVA中API:集合框架1(Collection,List,Set及其子类和迭代器的应用)
- Java多线程同步集合--并发库高级应用
- JAVA高级应用之集合 泛型 的使用
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
- 高级for循环在java集合中的应用
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
- Java学习总结(五)——(常用类,正则表达式,集合(List,Set) 迭代器)
- 在java商城开发中map集合的应用
- Java——集合IO综合应用
- Java面向对象高级--继承的应用----整形数组类、排序类、反转类
- java高级应用之框架篇
- Java基础---集合框架---迭代器、ListIterator、Vector中枚举、LinkedList、ArrayList、HashSet、TreeSet、二叉树、Comparator
- JavaWeb 第12章 JDBC详解(五)高级应用
- 黑马程序员_Java基础_集合框架工具类相关应用