JAVA基础: 集合\迭代器
来源:互联网 发布:iphone6淘宝店铺推荐 编辑:程序博客网 时间:2024/05/22 14:10
集合 : Collection
数组和集合的区别:
数组:
a.数组只能存相同数据类型的数据,可以存引用数据类型和基本数据类型;
b.数组一旦给定长度就不能改变长度了;
集合:
1.注意:集合中只能存引用数据类型(对象),也可以通过装箱来保存基本数据类型;
2.集合的长度可以发生变化;
当数据固定的时候可以用数组;
当数据不固定的时候可以用集合;
public class Student {private String name;private int age;//构造方法public Student() {}public Student(String name, int age) { super(); this.name = name; this.age = age;}//set/get方法public int getAge() { return age;}public void setAge(int age) { this.age = age;}public String getName() { return name;}public void setName(String name) { this.name = name;}//重写toString方法//直接打印student对象,就可以调用toString方法;@Overridepublic String toString() { return "Student [name=" + name + ", age=" + age + "]";}
}
public class demo{ public static void main (String[] args){ fun1; sun2; fun3; fun4; fun5; fun6; fun7; fun8; } public static void fun1() { //测试collection接口中的方法 Collection collection = new ArrayList(); collection.add("a"); collection.add(true); collection.add(100); collection.add(new Student("李宁",40)); System.out.println(collection); //打印结果:[a, true, 100, Student [name=李宁, age=18]] } public static void fun2(){ Collection collection = new ArrayList; collection.add("a"); collection.add("b"); collection.add("c"); collection.add("d"); //判断这个集合是否为空 boolean b1 = collection.isEmpty(); System.out.println(b1); //打印结果:false //获取集合长度 System.out.println(collection.size()); //打印结果:4 //判断是否包含某个元素 boolean b2 = collection.contains("z"); System.out.println(b2); //打印结果:false //删除某个元素 boolean b3 = collection.remove("a"); System.out.println(b3); //打印结果:true; //清空集合 collection.clear(); //打印集合元素 System.out.println(collection) } public static void fun3(){ Collection collection = new ArrayList; collection.add("a"); collection.add("b"); collection.add("c"); collection.add("d"); collection.toArray(); Objecr[] array = collection.toArray(); for(int i=0;i<array.length;i++){ System.out.println(array[i]); } } public static void fun4(){ Collection collection = new ArrayList(); collection.add(new Student("刘备",11));//向上转型 collection.add(new Student("张飞",12)); collection.add(new Student("",13)); //集合转数组 Object[] array = collection.toArray(); for (int i = 0; i < array.length; i++) { //向下转型 Student student = (Student)array[i]; System.out.println(student.getName()); } } //将集合里面所有的元素添加到另外一个集里面: 集合1名.addAll(集合2名); public static void fun5(){ //声明一个集合 Collection c1 = new Collection(); //添加集合里面的元素 c1.add("a"); c1.add("b"); c1.add("c"); c1.add("b"); //再创建一个集合 c2; Collection c2 = new Collection(); //创建c2里面的元素 c2.add("e"); c2.add("f"); //将c2里面的所有元素添加到c1里面"addAll" c1.addAll(c2); System.out.println(c1); //打印c1长度 System.out.println(c1.size()); } //判断两个集合是否有重复元素 public static void fun6() { Collection c1 = new ArrayList(); c1.add("a");// c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); c2.add("b");//判断全部包含是是可以有重复元素的 boolean b = c1.containsAll(c2); System.out.println(b); } public static void fun7() { Collection c1 = new ArrayList(); c1.add("a");// c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); c2.add("www"); //判断全部包含是是可以有重复元素的 //相当于把c1和c2的交集,在c1集合中删除 //删除时,可以有不相同的元素 c1.removeAll(c2); System.out.println(c1); System.out.println(c2); } public static void fun8() { Collection c1 = new ArrayList(); c1.add("a");// c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("v"); c2.add("x"); c2.add("x"); //c1转换成两个集合的交集取出来给 //如果c1改变了 就返回true //c1没改变返回false boolean b = c1.retainAll(c2); System.out.println(b); System.out.println(c1); }}
迭代器
public class Demo02 {
public static void main(String[] args) { //迭代器遍历时相当于有一个指针指向你的集合 //每next一次指针就向后移动 //最后遍历完成 Collection collection = new ArrayList(); collection.add("a");//相当于有个指针 collection.add("b"); collection.add("c"); collection.add("d"); fun1(collection); //使用迭代器循环遍历数组 //获取集合中的迭代器 fun2(); //向集合中添加三个学生进行遍历(使用迭代器) Collection collection = new ArrayList(); collection.add(new Student("linig",56)); collection.add(new Student("dfj",54)); collection.add(new Student("dhfjf",56)); //获取集合中迭代器 Iterator iterator = collection.iterator(); //循环遍历 while (iterator.hasNext()==true) { //next返回值是基础类 //获取集合中的元素一定要会 //向下转型 Student student = (Student)iterator.next(); System.out.println(student.getName()); }//重点iterator两个方法:hasNext\remove}public static void fun2() { Collection collection = new ArrayList(); collection.add("a");//相当于有个指针 collection.add("b"); collection.add("c"); collection.add("d"); Iterator iterator = collection.iterator(); while (iterator.hasNext()==true) { //获取集合中的元素一定要会 System.out.println(iterator.next()); }}public static void fun1(Collection collection) { //获取集合中的迭代器 Iterator iterator = collection.iterator(); //判断有没有下一个元素 boolean b1 = iterator.hasNext(); System.out.println(b1); //获取下一个元素 Object next = iterator.next(); System.out.println(next);}
}
阅读全文
0 0
- java基础 集合迭代器
- JAVA基础: 集合\迭代器
- Java基础14:集合类;list集合;迭代器;set集合;
- [ java ] java基础集合!
- java基础10:集合框架,迭代器,泛型
- JAVA基础之集合
- java基础之集合
- java基础:集合connection
- java基础之 集合
- java基础10 集合
- java基础--集合
- java基础---集合类
- java基础知识点集合
- JAVA基础之集合
- Java集合框架基础
- java基础--HashSet集合
- java基础_09_集合
- 【Java基础】集合
- mybatis动态插入sql语句
- 判断两条线段是否相交
- VUE—生成条形码
- 分而治之的算法(Devide and Conquer)
- Spring实现加密数据库连接
- JAVA基础: 集合\迭代器
- 一些常用JS方法
- C++11/14线程--线程的创建与分离
- 2017-12-17
- 压实测试(三)
- python多任务线程
- STM32 使用HAL库做串口的DMA发送和中断接收
- 个人提升:知识管理
- python 多进程