day15
集合
集合的介绍(引用数据类型数组)
package com.heima.collection;
import com.heima.bean.Student;
public class Demo1_Array {
public static void main(String[] args) {
Student[] arr = new Student[5];
arr[0] = new Student("张三", 23);
arr[1] = new Student("李四", 24);
arr[2] = new Student("王五", 25);
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
集合的概述
Collection集合的基本功能测试
package com.heima.collection;
import java.util.ArrayList;
import java.util.Collection;
import com.heima.bean.Student;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo2_Collection {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
System.out.println(c.size());
System.out.println(c);
}
public static void demo1() {
Collection c = new ArrayList();
boolean b1 = c.add("abc");
boolean b2 = c.add(true);
boolean b3 = c.add(100);
boolean b4 = c.add(new Student("张三", 23));
boolean b5 = c.add("abc");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
System.out.println(c.toString());
}
}
集合的遍历之集合转数组遍历
package com.heima.collection;
import java.util.ArrayList;
import java.util.Collection;
import com.heima.bean.Student;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo3_Collection {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(new Student("张三", 23));
c.add(new Student("李四", 24));
c.add(new Student("王五", 25));
c.add(new Student("赵六", 26));
Object[] arr = c.toArray();
for (int i = 0; i < arr.length; i++) {
Student s = (Student)arr[i];
System.out.println(s.getName() + "..." + s.getAge());
}
}
public static void demo1() {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
Object[] arr = c.toArray();
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Collection带All的功能
package com.heima.collection;
import java.util.ArrayList;
import java.util.Collection;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo4_CollectionAll {
public static void main(String[] args) {
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("c");
c2.add("g");
c2.add("e");
c2.add("f");
boolean b = c1.retainAll(c2);
System.out.println(b);
System.out.println(c1);
}
public static void demo3() {
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("z");
boolean b = c1.containsAll(c2);
System.out.println(b);
}
public static void demo2() {
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("z");
boolean b = c1.removeAll(c2);
System.out.println(b);
System.out.println(c1);
}
public static void demo1() {
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("c");
c2.add("d");
c1.add(c2);
System.out.println(c1);
}
}
集合的遍历之迭代器的遍历
package com.heima.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import com.heima.bean.Student;
public class Demo5_Iterator {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(new Student("张三", 23));
c.add(new Student("李四", 24));
c.add(new Student("王五", 25));
c.add(new Student("赵六", 26));
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "..." + s.getAge());
}
}
public static void demo1() {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
Iterator it = c.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
迭代器的原理及原码解析
A:迭代器原理
*迭代原理:迭代是对集合进行遍历,而每一个集合内部的存储结构都是不同的,所以每一个集合的存和取也都是不一样的,那么需要在每一个类中定义hasNext()和next()方法,这样做是可以的,但是会让整个集合体系过于臃肿,迭代器就是将这样的方法向上抽取出接口,然后每个类的内部,定义自己的迭代方法,这样做的好处有两个:一是规定了整个体系的迭代方法都是hasNext()和next()方法;二是:代码由顶层内部实现,使用者不管怎么实现的,会用即可;
B:迭代器原码解析
*1.在eclipse中ctrl+shift+t找到Arraylist类;
*2.ctrl+o找到iterator()方法;
*3.查看返回值类型是 new Itr(),说明这个类实现Iterator接口;
*4.查找Itr这个内部类,发现重写了Iterator中所有抽象方法
List接口(Collection接口)
List特有功能介绍
package com.heima.list;
import java.util.ArrayList;
import java.util.List;
public class Demo1_List {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.set(1, "z");
System.out.println(list);
}
public static void demo4() {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for(int i = 0;i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static void demo3() {
List list = new ArrayList();
list.add(111);
list.add(222);
list.add(333);
list.remove(111);
System.out.println(list);
}
public static void demo2() {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
Object obj = list.remove(1);
System.out.println(obj);
System.out.println(list);
}
public static void demo1() {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add(4, "f");
System.out.println(list);
}
}
List案例演示(List学生类遍历)
package com.heima.list;
import java.util.ArrayList;
import java.util.List;
import com.heima.bean.Student;
public class Demo2_List {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("张三", 23));
list.add(new Student("李四", 24));
list.add(new Student("王五", 25));
list.add(new Student("赵六", 26));
for (int i = 0; i < list.size(); i++) {
Student s = (Student) list.get(i);
System.out.println(s.getName() + "..." + s.getAge());
}
}
}
并发修改异常及解决方案(ConcurrentModificationException)
A:解决方案
*a:迭代器迭代元素,迭代器修改元素(ListInterator的特有功能add)
*b:集合遍历元素,集合修改元素;
B:案例演示
package com.heima.list;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Demo3_List {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("world");
list.add("c");
list.add("d");
list.add("e");
ListIterator lit = list.listIterator();
while (lit.hasNext()) {
String str = (String) lit.next();
if ("world".equals(str)) {
lit.add("javaee");
}
}
System.out.println(list);
}
}
ListInterator介绍
package com.heima.list;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Demo4_ListIterator {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("world");
list.add("c");
list.add("d");
list.add("e");
ListIterator lit = list.listIterator();
while (lit.hasNext()) {
System.out.print(lit.next() + " ");
}
System.out.println();
while (lit.hasPrevious()) {
System.out.print(lit.previous() + " ");
}
}
}
Vector特有功能(被ArrayList替代)
package com.heima.list;
import java.util.Enumeration;
import java.util.Vector;
public class Demo5_Vector {
public static void main(String[] args) {
Vector v = new Vector();
v.addElement("a");
v.addElement("b");
v.addElement("c");
v.addElement("d");
Enumeration en = v.elements();
while(en.hasMoreElements()) {
System.out.println(en.nextElement());
}
}
}
List三个子类的特点
数据结构部分介绍
数组和链表的区别
A:数组(相对链表比较)
*查询快,修改也快; 可快速找到索引
*增删慢; 增减相对移动耗时长
B:链表(相对数组比较)
*查询慢,修改也慢; 需要从链头到链尾一个一个查
*增删快 结点相互连接,删除空间