JavaSE学习总结第15天_集合框架1
15.01 对象数组的概述和使用
public class Student
{
// 成员变量
private String name;
private int age; // 构造方法
public Student()
{
super();
} public Student(String name, int age)
{
super();
this.name = name;
this.age = age;
} // 成员方法
// getXxx()/setXxx()
public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
} @Override
public String toString()
{
return "Student [name=" + name + ", age=" + age + "]";
}
}
/**
把5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
* 学生:Student
* 成员变量:name,age
* 构造方法:无参,带参
* 成员方法:getXxx()/setXxx()
* 分析:
* A:创建学生类。
* B:创建学生数组(对象数组)。
* C:创建5个学生对象,并赋值。
* D:把C步骤的元素,放到数组中。
* E:遍历学生数组。
* */ public class Practice
{
public static void main(String[] args)
{
// 创建学生数组(对象数组)。
Student[] students = new Student[5];
// for (int x = 0; x < students.length; x++)
// {
// System.out.println(students[x]);
// }
// System.out.println("---------------------"); // 创建5个学生对象,并赋值。
Student s1 = new Student("小明", 27);
Student s2 = new Student("小红", 30);
Student s3 = new Student("小强", 30);
Student s4 = new Student("旺财", 12);
Student s5 = new Student("张三", 35); // 将对象放到数组中。
students[0] = s1;
students[1] = s2;
students[2] = s3;
students[3] = s4;
students[4] = s5; // 遍历
for (int x = 0; x < students.length; x++)
{
//System.out.println(students[x]);
Student s = students[x];
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
15.02 对象数组的内存图解
15.03 集合的由来及与数组的区别
集合类的由来:面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。
数组和集合类同的区别:
数组可以存储同一种类型的基本数据也可以存储同一种类型的对象,但长度是固定的
集合只可以存储不同类型的对象,长度是可变的
集合类的特点:集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。
15.04 集合的继承体系图解
集合容器因为内部的数据结构不同,有多种具体容器,根据共性内容不断的向上抽取,就形成了集合框架。
框架的顶层Collection接口
15.05 Collection集合的功能概述
Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
15.06 Collection集合的基本功能测试
成员方法:
1. boolean add(E e):确保此 collection 包含指定的元素(可选操作)。
2. boolean remove(Object o):从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
3. void clear():移除此 collection 中的所有元素(可选操作)。
4. boolean contains(Object o):如果此 collection 包含指定的元素,则返回 true。
5. boolean isEmpty():如果此 collection 不包含元素,则返回 true。
6. int size():返回此 collection 中的元素数。
例:
// 创建集合对象
// Collection c = new Collection(); //错误,因为接口不能实例化
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
// c.clear();//移除所有元素
// System.out.println("remove:" + c.remove("hello"));//移除一个元素
// System.out.println("remove:" + c.remove("javaee"));
// 判断集合中是否包含指定的元素
System.out.println("contains:"+c.contains("hello"));//contains:true
System.out.println("contains:"+c.contains("android"));//contains:false
//判断集合是否为空
System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false
//元素的个数
System.out.println("size:"+c.size());//size:3
System.out.println("c:" + c);//c:[hello, world, java]
15.07 Collection集合的高级功能测试
成员方法:
1. boolean addAll(Collection<? extends E> c):
将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
2. boolean removeAll(Collection<?> c):
移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
3. boolean containsAll(Collection<?> c):
如果此 collection 包含指定 collection 中的所有元素,则返回 true。
4. boolean retainAll(Collection<?> c):
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。
例:
c1.addAll(c2);//将c2集合中的所有元素添加到c1集合中,c1变c2不变
c1.removeAll(c2);//将c1集合中与c2集合相同的所有元素删除,只要有一个相同的就返回true
c1.containsAll(c2);//判断c1集合中的元素是否包含c2中的全部元素,全部包含则返回true
c1.retainAll(c2);//将c1集合中与c2集合相同的元素保留,删除其他元素,返回值表示c1集合是否发生变化,发生变化返回true,没有变化返回false
15.08 集合的遍历之集合转数组遍历
Object[] toArray():返回包含此 collection 中所有元素的数组。
例:
public class Practice
{
public static void main(String[] args)
{
// 创建集合
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java"); Object[] objs = c.toArray();
for (int i = 0; i < objs.length; i++)
{
//向下转为String类型
String s = (String)objs[i];
System.out.println(s+":"+s.length());
}
}
}
运行结果:
hello:5
world:5
java:4
15.09 Collection存储自定义对象并遍历案例(使用数组)
例:
public class Practice
{
public static void main(String[] args)
{
// 创建集合
Collection c = new ArrayList();
//创建学生对象并添加到集合
c.add(new Student("小明",23));
c.add(new Student("小红",32));
c.add(new Student("小强",14));
c.add(new Student("旺财",8));
c.add(new Student("张三",16)); Object[] objs = c.toArray();
for (int i = 0; i < objs.length; i++)
{
Student s = (Student)objs[i];
System.out.println(s.getName()+":"+s.getAge());
}
}
}
运行结果:
小明:23
小红:32
小强:14
旺财:8
张三:16
15.10 集合的遍历之迭代器遍历
Iterator<E> iterator():返回在此 collection 的元素上进行迭代的迭代器。
例:
// 创建集合
Collection c = new ArrayList();
//创建元素并添加到集合
c.add("hello");
c.add("world");
c.add("java");
//获取迭代器,实际返回的是子类对象,多态
Iterator it = c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
15.11 Collection存储自定义对象并遍历案例(使用迭代器)
public class Practice
{
public static void main(String[] args)
{
// 创建集合
Collection c = new ArrayList();
//创建学生对象并添加到集合
c.add(new Student("小明",23));
c.add(new Student("小红",32));
c.add(new Student("小强",14));
c.add(new Student("旺财",8));
c.add(new Student("张三",16)); Iterator it = c.iterator();
while(it.hasNext())
{
Student s = (Student)it.next();
System.out.println(s.getName()+":"+s.getAge());
}
}
}
15.12 迭代器使用的问题探讨
1.使用迭代器获取元素的两种方式:
方式1:
Iterator it = c.iterator();
while(it.hasNext())
{
Student s = (Student)it.next();
System.out.println(s.getName()+":"+s.getAge());
}
方式2:
for(Iterator it = c.iterator();it.hasNext();)
{
Student s = (Student)it.next();
System.out.println(s.getName()+":"+s.getAge());
}
使用方式2的好处:it在for循环结束后就变成垃圾,效率较高
2.不要多次使用it.next()方法
例:
Iterator it = c.iterator();
while(it.hasNext())
{
System.out.println(((Student)it.next()).getName());
System.out.println(((Student)it.next()).getAge());
}
上面的代码表示获取的是第1个学生的姓名,第2个学生的年龄,以此类推,如果集合中的元素是奇数个,则会报NoSuchElementException错误
15.13 集合的使用步骤图解
集合的使用步骤:
1. 创建集合对象
2. 创建元素对象
3. 将元素添加到集合
4. 遍历集合
4.1 通过集合对象获取迭代器对象
4.2 通过迭代器对象的hasNext()方法判断是否有元素
4.3 通过迭代器对象的Next()方法获取元素并移动到下一个位置
15.14 迭代器的原理及源码解析
原理:
假设迭代器定义的是一个类,那么就可以创建该类的对象,调用该类的方法来实现集合的遍历,但是Java中提供了很多的集合类,而这些集合类的数据结构是不同的,所以存储的方式和遍历的方式也应该是不同的,进而它们的遍历方式也应该是不一样的。最终就没有定义迭代器类
而无论使用哪种集合都应该具备获取元素的操作,并且最好再辅助与判断功能,也就是说判断功能和获取功能应该是一个集合遍历所具备的,而每种集合的方式又不太一样,所以将这两个功能给提取出来,并不提供具体实现,这种方式就是接口,而真正具体的实现类在具体的子类中以内部类的方式体现的
源码:
public interface Iterator
{
boolean hasNext();
Object next();
}
public interface Iterable
{
Iterator iterator();
}
public interface Collection extends Iterable
{
Iterator iterator();
}
public interface List extends Collection
{
Iterator iterator();
}
public class ArrayList implements List
{
public Iterator iterator()
{
return new Itr();
}
//内部类
private class Itr implements Iterator
{
public boolean hasNext() {}
public Object next(){}
}
} Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator(); //new Itr();
while(it.hasNext())
{
String s = (String)it.next();
System.out.println(s);
}
15.15 Collection存储字符串并遍历
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; public class Practice
{ public static void main(String[] args)
{ // 创建集合
Collection c = new ArrayList(); //添加字符串
c.add("hello");
c.add("你好");
c.add("world");
c.add("java");
c.add("旺财");
//通过集合对象获取迭代器对象 Iterator it = c.iterator();
while(it.hasNext()) {
String s = (String)it.next(); System.out.println(s);
} } }
15.16 Collection存储学生对象并遍历
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; public class Practice
{
public static void main(String[] args)
{
// 创建集合
Collection c = new ArrayList();
//创建学生对象并添加到集合
c.add(new Student("小明",23));
c.add(new Student("小红",32));
c.add(new Student("小强",14));
c.add(new Student("旺财",8));
c.add(new Student("张三",16)); Iterator it = c.iterator();
while(it.hasNext())
{
Student s = (Student)it.next();
System.out.println(s.getName()+":"+s.getAge());
}
}
}
15.17 List存储字符串并遍历
public class Practice
{
public static void main(String[] args)
{
// 创建集合
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java"); Iterator it = list.iterator();
while(it.hasNext())
{
String s = (String)it.next();
System.out.println(s);
}
}
}
15.18 List集合的特点
List接口概述:有序的(存取顺序一致)collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
特点:与 set 不同,列表通常允许重复的元素。
15.19 List存储学生对象并遍历
public class Practice
{
public static void main(String[] args)
{
// 创建集合
List list = new ArrayList();
//创建学生对象并添加到集合
list.add(new Student("小明",23));
list.add(new Student("小红",32));
list.add(new Student("小强",14));
list.add(new Student("旺财",8));
list.add(new Student("张三",16)); Iterator it = list.iterator();
while(it.hasNext())
{
Student s = (Student)it.next();
System.out.println(s.getName()+":"+s.getAge());
}
}
}
15.20 List集合的特有功能概述和测试
1. void add(int index,E element):
在列表的指定位置插入指定元素(可选操作)。
2. E remove(int index):
移除列表中指定位置的元素(可选操作)。
3. E get(int index):
返回列表中指定位置的元素。
4. E set(int index, E element):
用指定元素替换列表中指定位置的元素(可选操作)。
例:
list.add(2,"javaee");//在2的位置插入javaee,改变集合长度
list.get(2)//返回集合中2位置上的元素,不改变集合长度
list.remove(1)//删除集合中1位置上的元素,返回被删除的元素,改变集合长度
list.set(2, "javaee")//将集合中2位置上的元素替换为javaee,返回被替换的元素,不改变集合长度
15.21 List集合的特有遍历功能
for (int i = 0; i < list.size(); i++) { String s = (String)list.get(i); System.out.println(s); }
15.22 List存储自定义对象并遍历(使用List特有功能遍历)
public class Practice
{
public static void main(String[] args)
{
// 创建集合
List list = new ArrayList();
//创建学生对象并添加到集合
list.add(new Student("小明",23));
list.add(new Student("小红",32));
list.add(new Student("小强",14));
list.add(new Student("旺财",8));
list.add(new Student("张三",16)); for (int i = 0; i < list.size(); i++)
{
Student s =(Student)list.get(i);
System.out.println(s.getName()+":"+s.getAge());
}
}
}
15.23 ListIterator的特有功能
ListIterator<E> listIterator():
返回此列表元素的列表迭代器(按适当顺序)。
注意:ListIterator可以实现逆向遍历,但是必须先正向遍历,才能逆向遍历
例:
public class Practice
{
public static void main(String[] args)
{
// 创建集合
List list = new ArrayList(); list.add("hello");
list.add("world");
list.add("java");
//列表迭代器
ListIterator lit = list.listIterator();
//正向遍历
while(lit.hasNext())
{
String s = (String)lit.next();
System.out.println(s);
}
System.out.println("-----");
//逆向遍历
while(lit.hasPrevious())
{
//获取上一个元素
String s = (String)lit.previous();
System.out.println(s);
} }
}
运行结果:
hello
world
java
-----
java
world
hello
15.24 并发修改异常的产生原因及解决方案
例:
public class Practice
{
public static void main(String[] args)
{
// 创建集合
List list = new ArrayList(); list.add("hello");
list.add("world");
list.add("java");
Iterator it = list.iterator();
while(it.hasNext())
{
String s = (String)it.next();
if(s.equals("world"))
list.add("javaee");
}
System.out.println(list);
}
}
上面的代码会运行错误,发生ConcurrentModificationException异常
错误产生原因:迭代器是依赖于集合存在的,在迭代的过程中使用集合的方法添加元素迭代器是不知道的,所以报错,并发修改异常
解决方案:1.用迭代器迭代元素时使用迭代器修改元素(ListIterator列表迭代器),添加的元素在迭代的元素后面
2.用集合遍历元素,用集合修改元素(for循环),添加的元素在最后
15.25 数据结构之栈和队列
数据结构:数据的组织方式
15.26 数据结构之数组和链表
数组:存储同一种类型的多个元素的容器
链表:由一个链子把多个节点(数据和节点)连起来组成的数据
15.27 List的三个子类的特点
ArrayList:底层数据结构是数组,查询快,增删慢,是不同步的,线程不安全,效率高
Vector:底层数据结构是数组,查询快,增删慢,是同步的,线程安全,效率低
LinkedList:底层数据结构是链表,查询慢,增删快,是不同步的,线程不安全,效率高
JavaSE学习总结第15天_集合框架1的更多相关文章
- JavaSE学习总结第16天_集合框架2
16.01 ArrayList存储字符串并遍历 ArrayList类概述:底层数据结构是数组,查询快,增删慢,线程不安全,效率高 ArrayList类是List 接口的大小可变数组的实现.实现了所 ...
- JavaSE学习总结第17天_集合框架3
17.01 ArrayList集合的toString()方法源码解析 代码: Collection c = new ArrayList(); c.add("hello"); c ...
- JavaSE学习总结第18天_集合框架4
18.01 Map集合概述和特点 Map接口概述:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值 Map接口和Collection接口的不同 1.Map是双列的,Coll ...
- javaSE学习笔记(15) ---缓冲流、转换流、序列化流
javaSE学习笔记(15) ---缓冲流.转换流.序列化流 缓冲流 昨天复习了基本的一些流,作为IO流的入门,今天我们要见识一些更强大的流.比如能够高效读写的缓冲流,能够转换编码的转换流,能够持久化 ...
- JavaSE学习总结第27天_反射 & 设计模式 & JDK5、7、8新特性
27.01 反射_类的加载概述和加载时机 类的加载:当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载:就是指将class文件读 ...
- Java基础学习(四)-- 接口、集合框架、Collection、泛型详解
接口 一.接口的基本概念 关键字为:Interface,在JAVA编程语言中是一个抽象类型,是抽象方法的集合.也是使用.java文件编写. 二.接口声明 命名规范:与类名的命名规范相同,通常情况下 ...
- Java之旅_高级教_集合框架
摘自:http://www.runoob.com/java/java-collections.html Java 集合框架 早在Java2之前,java 就提供了特设类.比如:Dictionary,V ...
- Java学习日记基础篇(九) —— 集合框架,泛型,异常
集合框架 有事我们会需要一个能够动态的调整大小的数组,比如说要添加新员工但是数组已经满了,并且数组的大小是在定义的时候定死的,所以我们就需要一个能够动态调整大小的数组或者用链表解决,而java中提供了 ...
- java oop第07章_集合框架
一. 什么是集合: 在Java中提供了一些可以保存同一数据类型的数据集称为集合,就是规定了一些集合的规范(接口.抽象类.实现类)及方法, 方便我们程序在保存数据时进行增.删.改.查操作,编程更加高效. ...
随机推荐
- querySelector $() getElementBy区别
参考 http://stackoverflow.com/questions/14377590/queryselector-and-queryselectorall-vs-getelementsbycl ...
- npm使用教程(未完)
npm docs 设置镜像站 因为npmjs的官方网站,总会下载比较慢或打不开,所以通常需要设置一下镜像站来更好的安装npm库 npm install --registry http://regist ...
- HDU 5800 To My Girlfriend(单调DP)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5800 [题目大意] 给出一个容量上限s,f[i][j][k][l][m]表示k和l两个物品不能选,i ...
- 夜未央Test1题解
T1 积木游戏 树状数组的一个简单应用,建立一个维护左节点的树状数组和一个维护右节点的树状数组,对于add操作,只要在维护左节点的树状数组l处加1,维护右节点的树状数组r处加 ...
- java单链表代码实现
用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...
- UVA122-Trees on the level(链二叉树)
Trees on the level Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Sub ...
- Node.js初学
Node.js 初学~ 其技术上最大的卖点是非阻塞的I/O和基于事件的异步处理机制. 后端没有什么深入研究,一直对其不是很了解. 透过一个例子看 非阻塞 与 通常的 阻塞 var text = rea ...
- zoj 1083 Frame Stacking
其实就是一个拓补排序.(动态记录第i个之上的j存不存在,反过来就是第j个之下的i) 首先确立每个框的位置(题目明确说了每一边都不会被完全覆盖)./*可以通过搜索,搜索到该框的所有四个角*/||如果题目 ...
- ViewState是什么
在做ASP.NET的时候遇到ViewState,当时不知道他是什么意思. 就在当前页面中保存数据的. 像session.是会话级别的.只要会话没有过期.session中存的数据就在. viewstat ...
- Unity Editor下对资源进行操作时调用AssetModificationProcessor
public class Test : UnityEditor.AssetModificationProcessor { private static void OnWillCreateAsset(s ...