1 /*
2 * 集合的由来:
3 * 我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储。
4 * 而要想存储多个对象,就不能是一个基本的变量,而应该是一个容器类型的变量,在我们目前所学过的知识里面,有哪些是容器类型的呢?
5 * 数组和StringBuffer。但是呢?StringBuffer的结果是一个字符串,不一定满足我们的要求,所以我们只能选择数组,这就是对象数组。
6 * 而对象数组又不能适应变化的需求,因为数组的长度是固定的,这个时候,为了适应变化的需求,Java就提供了集合类供我们使用。
7 *
8 * 数组和集合的区别?
9 * A:长度区别
10 * 数组的长度固定
11 * 集合长度可变
12 * B:内容不同
13 * 数组存储的是同一种类型的元素
14 * 而集合可以存储不同类型的元素
15 * C:元素的数据类型问题
16 * 数组可以存储基本数据类型,也可以存储引用数据类型
17 * 集合只能存储引用类型
18 *
19 * 刚说过集合是存储多个元的,但是呢,存储多个元素我们也是有不同需求的:比如说,我要这多个元素中不能有相同的元素,
20 * 再比如说,我要这多个元素按照某种规则排序一下。针对不同的需求,Java就提供了不同的集合类,这样呢,Java就提供了很多个集合类。
21 * 这多个集合类的数据结构不同,结构不同不重要的,重要的是你要能够存储东西,并且还要能够使用这些东西,比如说判断,获取等。
22 * 既然这样,那么,这多个集合类是有共性的内容的,我们把这些集合类的共性内容不断的向上提取,最终就能形成集合的继承体系结构。
23 *
24 * 数据结构:数据的存储方式。
25 *
26 * Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。(后面会慢慢的讲解)
27 *
28 * Collection的功能概述:
29 * 1:添加功能
30 * boolean add(Object obj):添加一个元素
31 * boolean addAll(Collection c):添加一个集合的元素
32 * 2:删除功能
33 * void clear():移除所有元素
34 * boolean remove(Object o):移除一个元素
35 * boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
36 * 3:判断功能
37 * boolean contains(Object o):判断集合中是否包含指定的元素
38 * boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
39 * boolean isEmpty():判断集合是否为空
40 * 4:获取功能
41 * Iterator<E> iterator()(重点)
42 * 5:长度功能
43 * int size():元素的个数
44 * 面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
45 * 6:交集功能
46 * boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
47 * 7:把集合转换为数组
48 * Object[] toArray()
49 */
1 public class CollectionTest {
2 public static void main(String[] args) {
3 // 创建集合对象
4 // Collection c = new Collection(); //错误,因为接口不能实例化
5 Collection c=new ArrayList<>();
6 c.add("Hello");
7 c.add("World");
8 //c.clear();
9 //c.remove("World");
10
11 System.out.println(c.contains("Hello"));
12 System.out.println(c.size());
13 System.out.println(c.isEmpty());
14 }
15 }
/* java 迭代器使用
* 练习:用集合存储5个学生对象,并把学生对象进行遍历。用迭代器遍历。
*
* 注意:
* A:自己的类名不要和我们学习的要使用的API中的类名相同。
* B:复制代码的时候,很容易把那个类所在的包也导入过来,容易出现不能理解的问题。
*/
public class IteratorTest {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
// 把学生添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 遍历
Iterator it = c.iterator();
while (it.hasNext()) {
// System.out.println(it.next());
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
for(Iterator it = c.iterator();it.hasNext();){
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
List:(面试题List的子类特点)
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
List有三个儿子,我们到底使用谁呢?
看需求(情况)。
要安全吗?
要:Vector(即使要安全,也不用这个了,后面有替代的)
不要:ArrayList或者LinkedList
查询多:ArrayList
增删多:LinkedList
如果你什么都不懂,就用ArrayList。
/*
* 需求:List集合存储字符串并遍历。
*/
public class ListDemo {
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);
}
}
}
/*
* 存储自定义对象并遍历
*/
public class ListDemo {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 创建学生对象
Student s1 = new Student("白骨精", 30);
Student s2 = new Student("蜘蛛精", 40);
Student s3 = new Student("观音姐姐", 22);
// 把学生对象添加到集合对象中
list.add(s1);
list.add(s2);
list.add(s3);
// 遍历
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}