您的位置:首页 > 编程语言 > Java开发

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());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐