Collection 和 Iterator
来源:互联网 发布:添加数组中的指定元素 编辑:程序博客网 时间:2024/06/06 14:13
Collection 和 Iterator
代表的是顶层次的一个根接口! jdk不提供此接口的任何直接实现,而是子接口的具体类进行实现!
和添加功能相关的方法:
boolean add(Object obj):添加一个元素
boolean addAll(Collection c):添加一个集合中的元素
和删除功能相关的方法:
boolean remove(Object o):删除指定的元素
boolean removeAll(Collection c):删除一个集合中的元素(思考:删除一个元素算是删除呢还是删除所有算是删除?)
void clear():删除一个集合中的所有元素
和判断功能相关的方法:
boolean contains(Object o):判断该集合中是否包含指定的元素
boolean containsAll(Collection c):判断是否包含一个集合中的元素(包含一个算是包含,还是包含所有元素算术包含?)
boolean isEmpty():判断集合是否为空
长度功能:
int size()
面试题:数组中有没有length()方法?String类有没有length()方法?集合中有没有length()方法?
数组:length属性
字符串String:length()
集合:size()
Iterator iterator():集合的专有迭代遍历方式
交集功能:
boolean retainAll(Collection c):对一个集合取交集(思考:返回值类型表达的是什么意思?交集的元素去哪里?)
集合的转换功能:
Object[] toArray():将集合转换数组
代码:
public static void main(String[] args) { //创建集合 Collection c = new ArrayList(); Collection c2= new ArrayList(); //添加 c.add("hello"); c.add("world"); c.add("hello"); c.add("java"); c2.add("hello"); c2.add("love"); System.out.println(c);//底部已经重写了toString()方法 //删除功能 /* 和删除功能相关的方法: * boolean remove(Object o):删除指定的元素 * boolean removeAll(Collection c):删除一个集合中的元素(思考:删除一个元素算是删除呢还是删除所有算是删除?) * void clear():删除一个集合中的所有元素 */ System.out.println(c.remove("hello"));//只要删除了就返回true System.out.println(c); System.out.println(c.removeAll(c2)); System.out.println(c); /* * 和判断功能相关的方法:
* boolean contains(Object o):判断该集合中是否包含指定的元素
* boolean containsAll(Collection c):判断是否包含一个集合中的元素(包含一个算是包含,还是包含所有元素算术包含?)
* boolean isEmpty():判断集合是否为空
* */
System.out.println(c.isEmpty());
System.out.println(c.containsAll(c2));//包含全部算包含
/* * 长度功能: int size() * */ System.out.println(c.size()); /* * 交集功能:
* boolean retainAll(Collection c):对一个集合取交集(思考:返回值类型表达的是什么意思?交集的元素去哪里?)
* */
System.out.println(c);
System.out.println(c2);
System.out.println(c.retainAll(c2));//交集元素存在c集合中,boolean表达的意思是c集合中的元素有没有发生变化
System.out.println(c);
}
结果
[hello, world, hello, java]
true
[world, hello, java]
true
[world, java]
false
false
2
[world, java]
[hello, love]
true
[]
需求;
Obejct[] toArray():把集合转换成数组
代码:
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello"); c.add("mu"); c.add("yang"); Object[] obj= c.toArray(); System.out.println(obj);//[Ljava.lang.Object;@79f1d448 //遍历 for (int x =0;x<obj.length;x++){ String s = (String)obj[x]; System.out.println(s); }}
结果:
[Ljava.lang.Object;@31731334
hello
mu
yang
需求:
给集合中存储4个学生对象,最终要把5个学生的信息遍历出来!
代码:
public static void main(String[] args) {
Collection c= new ArrayList();
sdudent s1 = new sdudent("张三",20,"188"); sdudent s2 = new sdudent("李四",21,"186"); sdudent s3 = new sdudent("王五",22,"185"); c.add(s1); c.add(s2); c.add(s3); Iterator i = c.iterator(); for(int x =0;x<c.size();x++){ sdudent s = (sdudent) i.next(); System.out.println(s.getName()+"-"+s.getAge()+"-"+s.getWeight()); }}
结果
张三-20-188
李四-21-186
王五-22-185
- java--collection和Iterator
- Collection 和 Iterator
- Collection和Iterator接口
- Collection接口和Iterator接口
- Collection和Iterator学习笔记
- Collection Iterator
- linkin大话数据结构--Collection和Iterator
- Java编程思想11.12-Collection和Iterator
- Java中ListIterator和Iterator与collection
- collection、iterator和map的解析
- Java中Collection和Iterator接口
- Iterator的remove()和Collection的remove()
- Iterator的remove()和Collection的remove()
- Java_集合—Collection和Iterator
- Iterator的remove()和Collection的remove()
- Collection和Iterator接口方法详解
- Iterator的remove()和Collection的remove()
- 4.2.2集合类 Collection接口和 Iterator接口
- 检测和识别身份证的关键字段内容。(face++身份证信息识别)
- splay详解
- NSURL 的几个属性
- 《gradle 用户指南》中文版 第3章 安装 gradle
- Java开发为什么要配置环境变量?
- Collection 和 Iterator
- 在SQL Server中快速删除重复记录
- 素因数分解
- ACM/ICPC CERC 1997,UVa 514,POJ 1363 Rails(栈模拟)
- 对NOSQL学习的理解
- 将caffe变成crop长宽不同的大小
- Mongo进阶--Security安全介绍
- 修改Tomcat 端口号
- 利用sklearn训练LDA主题模型及调参详解