15 API-集合(Collection(功能,迭代器),List(List特有迭代器,并发异常),常见数据结构图示(栈,队列,数组,链表))&对象数组

来源:互联网 发布:2017淘宝天猫商家数目 编辑:程序博客网 时间:2024/05/01 01:23

1:对象数组(掌握)

(1)数组既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的时候的数组就叫对象数组。


(2)案例:

用数组存储5个学生对象,并遍历数组。

学生的对象

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;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}}

对象数组存数5个学生的对象并遍历

/* * 我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。 * 学生:Student * 成员变量:name,age * 构造方法:无参,带参 * 成员方法:getXxx()/setXxx() * 存储学生的数组?自己想想应该是什么样子的? * 分析: * A:创建学生类。 * B:创建学生数组(对象数组)。 * C:创建5个学生对象,并赋值。 * D:把C步骤的元素,放到数组中。 * E:遍历学生数组。 */public class ObjectArrayDemo {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("赵雅芝", 60);Student s5 = new Student("王力宏", 35);// 把C步骤的元素,放到数组中。students[0] = s1;students[1] = s2;students[2] = s3;students[3] = s4;students[4] = s5;// 看到很相似,就想循环改// for (int x = 0; x < students.length; x++) {// students[x] = s + "" + (x + 1);// }// 这个是有问题的// 遍历for (int x = 0; x < students.length; x++) {//System.out.println(students[x]);Student s = students[x];System.out.println(s.getName()+"---"+s.getAge());}}}



2:集合(Collection)(掌握)


       (1)集合的由来?

              我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer)-- 数组

              而数组的长度固定,所以不适合做变化的需求,Java就提供了集合供我们使用。

       (2)集合和数组的区别?

              A:长度区别

                     数组固定

                     集合可变

              B:内容区别

                     数组可以是基本类型,也可以是引用类型

                     集合只能是引用类型

              C:元素内容

                     数组只能存储同一种类型

                     集合可以存储不同类型(其实集合一般存储的也是同一种类型)

       (3)集合的继承体系结构?

              由于需求不同,Java就提供了不同的集合类。这多个集合类的数据结构不同,但是它们都是要提供存储和遍历功能的,

              我们把它们的共性不断的向上提取,最终就形成了集合的继承体系结构图。


             

              Collection

                     |--List

                            |--ArrayList

                            |--Vector

                            |--LinkedList

                     |--Set

                            |--HashSet

                            |--TreeSet




(4)Collection的功能概述()

A:添加功能 

boolean add(Object obj):添加一个元素
boolean addAll(Collection c):添加一个集合的元素
  

B:删除功能

void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素

C:判断功能

boolean contains(Object o):判断集合中是否包含指定的元素
 * boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(只有包含所有的元素,才叫包含)
 * boolean isEmpty():判断集合是否为空

D:获取功能

Iterator<E> iterator()(重点)

E:长度功能

int size():元素的个数
 * 面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?

数组:.length      字符串:.length()         集合: .size()

F:交集(了解)

boolean retainAll(Collection c):A集合对B集合做交集,最终的结果保存在A中,B不变。返回值表示的是A是否发生过改变。

G:把集合转数组(了解)

Object[] toArray()

(5)Collection集合的遍历
A:把集合转数组(了解)
B:迭代器(集合专用方式)

(6)迭代器

A:是集合的获取元素的方式。

Iterator iterator():迭代器,集合的专用遍历方式
 * Object next():获取元素,并移动到下一个位置。
 * NoSuchElementException:没有这样的元素,因为你已经找到最后了。

NoSuchElementException 不要多次使用it.next()方法
 * boolean hasNext():如果仍有元素可以迭代,则返回 true。

B:是依赖于集合而存在的。
C:迭代器的原理和源码。

a:为什么定义为了一个接口而不是实现类?


b:看了看迭代器的内部类实现。

简易内部源码

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);}



       (7)Collection集合的案例(遍历方式迭代器)

              集合的操作步骤:

                     A:创建集合对象

                     B:创建元素对象

                     C:把元素添加到集合

                     D:遍历集合

      

              A:存储字符串并遍历

                     importjava.util.Collection;

                     importjava.util.ArrayList;

                     importjava.util.Iterator;

                    

                     publicclass CollectionDemo {

                            publicstatic void main(String[] args) {

                                   //创建集合对象

                                   Collectionc = new ArrayList();

                                  

                                   //创建并添加元素

                                   c.add("hello");

                                   c.add("world");

                                   c.add("java");

                                  

                                   //遍历集合

                                   Iteratorit = c.iterator();

                                   while(it.hasNext()){

                                          Strings =(String) it.next();

                                          System.out.println(s);

                                   }

                            }

                     }

             

              B:存储自定义对象并遍历

                     publicclass Student {

                            privateString name;

                            privateint age;

                           

                            publicStudent(){}

                           

                            publicStudent(String name,int age) {

                                   this.name= name;

                                   this.age= age;

                            }

                           

                            //getXxx()/setXxx()

                     }

                    

                     importjava.util.Collection;

                     importjava.util.ArrayList;

                     importjava.util.Iterator;

                    

                     publicclass StudentDemo {

                            publicstatic void main(String[] args) {

                                   //创建集合对象

                                   Collectionc = new ArrayList();

                                  

                                   //创建学生对象

                                   Students1 = new Student("林青霞",27);

                                   Students2 = new Student("风清扬",30);

                                   Students3 = new Student("刘意",30);

                                   Students4 = new Student("武鑫",25);

                                   Students5 = new Student("刘晓曲",16);

                                  

                                   //添加元素

                                   c.add(s1);

                                   c.add(s2);

                                   c.add(s3);

                                   c.add(s4);

                                   c.add(s5);

                                  

                                   //遍历集合

                                   Iteratorit = c.iterator();

                                   while(it.hasNext()){

                                          Students = (Student)it.next();

                                          System.out.println(s.getName()+"---"+s.getAge());

                                   }

                            }

                     }



3:集合(List)(掌握)

(1)List是Collection的子接口
特点:有序(存储顺序和取出顺序一致),可重复。
(2)List的特有功能:()

A:添加功能

void add(int index,Object element):在指定位置添加元素

B:删除功能

Object remove(int index):根据索引删除元素,返回被删除的元素

C:获取功能

Object get(int index):获取指定位置的元素

D:迭代器功能

ListIterator listIterator():List集合特有的迭代器

E:修改功能

Object set(int index,Object element):根据索引修改元素,返回被修饰的元素

(3)List集合的特有遍历功能
A:由size()和get()结合。
B:代码演示
//创建集合对象
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);
}
System.out.println("----------");

for(int x=0; x<list.size(); x++) {
String s =(String) list.get(x);
System.out.println(s);
}
(4)列表迭代器的特有功能;(了解)

boolean hasPrevious()  +++++++   E previous()  配合逆向迭代
可以逆向遍历,但是要先正向遍历,所以无意义,基本不使用。

void add(E e)将指定的元素插入列表(可选操作)。
void set(E e)指定元素替换 nextprevious 返回的最后一个元素(可选操作)。
void remove()从列表中移除由 nextprevious 返回的最后一个元素(可选操作)。


(5)并发修改异常
A:出现的现象  ---并发修改异常
迭代器遍历集合,集合修改集合元素
B:原因
迭代器是依赖于集合的,而集合的改变迭代器并不知道。
C:解决方案
a:迭代器遍历,迭代器修改(ListIterator)
元素添加在刚才迭代的位置
b:集合遍历,集合修改(size()和get())
元素添加在集合的末尾

(6)常见数据结构

A:栈 先进后出

B:队列 先进先出
C:数组 查询快,增删慢

D:链表 查询慢,增删快

栈和队列


数组和链表


(7)List的子类特点(面试题)
ArrayList
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。

到底使用谁呢?看需求?
分析:
要安全吗?
要:Vector(即使要,也不使用这个,后面再说)
不要:ArrayList或者LinkedList
查询多;ArrayList
增删多:LinkedList

什么都不知道,就用ArrayList。
(8)List集合的案例(遍历方式 迭代器和普通for)
A:存储字符串并遍历
B:存储自定义对象并遍历
0 0
原创粉丝点击