Java EE Fundamentals (15)/collections

Source: Internet
Author: User

1. Collection Framework (Overview and use of object arrays)
    • A: Case Demo
      • Requirement: I have 5 students, please store the information of the 5 students in an array, and iterate through the array to get each student information.
    • Student[] arr = new Student[5];                 //存储学生对象arr[0] = new Student("张三", 23);arr[1] = new Student("李四", 24);arr[2] = new Student("王五", 25);arr[3] = new Student("赵六", 26);arr[4] = new Student("马哥", 20);for (int i = 0; i < arr.length; i++) {    System.out.println(arr[i]);}
    • B: Drawing Demo

      • Explain the case of the student array
      • Arrays and collections store reference data types, all of which are address values
2. Set FRAME (collection origin and collection inheritance system diagram)
    • A: The origin of the collection
      • The length of the array is fixed, when the added element exceeds the length of the array needs to be redefined, too cumbersome, Java provides us with a collection class, can store arbitrary objects, the length can be changed, as the element increases, and decreases as the element decreases
    • B: The difference between an array and a set
      • Difference 1:
        • Arrays can store both the base data type and the reference data type, the base data type stores the value, and the reference data type stores the address value
        • A collection can store only reference data types (objects) in a collection or store basic data types, but it is automatically boxed into objects when stored
      • Difference 2:
        • The array length is fixed and does not grow automatically
        • The length of the collection is variable and can grow depending on the element's increase
    • C: Arrays and collections when to use * 1, if the number of elements is fixed recommended with array * 2, if the number of elements is not a fixed recommended set
    • D: Set Inheritance system diagram
3. Set FRAME (basic function test of collection collection)
    • A: Case Demo
    • 基本功能演示boolean add(E e)boolean remove(Object o)void clear()boolean contains(Object o)boolean isEmpty()int size()
    • B: Note:

    • collectionXxx.java使用了未经检查或不安全的操作.注意:要了解详细信息,请使用 -Xlint:unchecked重新编译.java编译器认为该程序存在安全隐患温馨提示:这不是编译失败,所以先不用理会,等学了泛型你就知道了
4. Set FRAME (set traversal of collection to array traversal)
    • A: Traversal of the collection
      • It is actually getting each element in the collection in turn.
    • B: Case Demo

      • The collection can be traversed by transferring the set to the array.
      • ToArray () *

        Collection coll = new ArrayList();coll.add(new Student("张三",23));     //Object obj = new Student("张三",23);coll.add(new Student("李四",24));coll.add(new Student("王五",25));coll.add(new Student("赵六",26));Object[] arr = coll.toArray();              //将集合转换成数组for (int i = 0; i < arr.length; i++) {    Student s = (Student)arr[i];            //强转成Student    System.out.println(s.getName() + "," + s.getAge());}
5. Set frame (with all function test for collection collection)
    • A: Case Demo
    • 带All的功能演示boolean addAll(Collection c)boolean removeAll(Collection c)boolean containsAll(Collection c)boolean retainAll(Collection c)
6. Set FRAME (traversal of collection iterator traversal)
    • A: An overview of iterators
      • The collection is used to store elements, and the stored elements need to be viewed, then iterate (Traverse)
    • B: Case Demo

      • Use of iterators

        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());}
7. Collection Framework (collection store custom objects and traverse)
    • A: Case Demo

      • Collection Store custom objects and iterate through iterators
      • Collection c = new ArrayList();c.add(new Student("张三",23));c.add(new Student("李四",24));c.add(new Student("王五",25));c.add(new Student("赵六",26));c.add(new Student("赵六",26));for(Iterator it = c.iterator();it.hasNext();) {    Student s = (Student)it.next();                     //向下转型    System.out.println(s.getName() + "," + s.getAge()); //获取对象中的姓名和年龄}System.out.println("------------------------------");Iterator it = c.iterator();                             //获取迭代器while(it.hasNext()) {                                   //判断集合中是否有元素    //System.out.println(((Student)(it.next())).getName() + "," + ((Student)(it.next())).getAge());    Student s = (Student)it.next();                     //向下转型    System.out.println(s.getName() + "," + s.getAge()); //获取对象中的姓名和年龄}
8, set frame (the principle of the iterator and source code analysis)
    • A: The principle of iterators
      • The iterator principle: the iterator is to iterate over the collection, and each collection inside the storage structure is different, so each collection and fetch are not the same, then you need to define the Hasnext () and Next () method in each class, it is possible, but will make the whole collection system is too bloated, Iterators are the way to pull up the interface, and then within each class, define their own iterative way, the benefits of this is two, the first rule that the entire collection system is Hasnext () and Next () method, the second, the code has a bottom-level internal implementation, users do not control how to achieve, You can use it.
    • B: Iterator Source parsing
      • 1, in Eclipse CTRL + SHIFT + T to find the ArrayList class
      • 2,ctrl+o Find Iterator () method
      • 3, view the return value type is new Itr (), indicating Itr This class implements iterator interface
      • 4, find ITR This inner class, found rewriting all the abstract methods in iterator
9. Collection Framework (overview and testing of unique functions of the list collection)
    • Overview of the unique features of the A:list collection
      • void Add (int index,e Element)
      • E Remove (int index)
      • E get (int index)
      • E set (int index,e element)
10. Set FRAME (List collection to store student objects and traverse)
    • A: Case Demo

      • The traversal is used in conjunction with the size () and get () methods.

        List list = new ArrayList();list.add(new Student("张三", 18));list.add(new Student("李四", 18));list.add(new Student("王五", 18));list.add(new Student("赵六", 18));for(int i = 0; i < list.size(); i++) {    Student s = (Student)list.get(i);    System.out.println(s.getName() + "," + s.getAge());}
11. Set frame (causes and solutions of concurrency modification exceptions)
    • A: Case Demo

      • Requirement: I have a collection, ask, I want to judge inside there is no "world" this element, if there is, I will add a "Java ee" element, please write code implementation.

        List list = new ArrayList();list.add("a");list.add("b");list.add("world");list.add("d");list.add("e");/*Iterator it = list.iterator();while(it.hasNext()) {    String str = (String)it.next();    if(str.equals("world")) {        list.add("javaee");         //这里会抛出ConcurrentModificationException并发修改异常    }}*/
    • B:concurrentmodificationexception appears

      • Iterator traversal, collection modification collection
    • C: Solutions

      • A: Iterator iteration element, iterator modification element (Listiterator unique function Add)
      • B: Set traversal element, set modify element

        ListIterator lit = list.listIterator();     //如果想在遍历的过程中添加元素,可以用ListIterator中的add方法while(lit.hasNext()) {    String str = (String)lit.next();    if(str.equals("world")) {        lit.add("javaee");          //list.add("javaee");    }}
12. Set FRAME (listiterator)
    • Boolean hasnext () whether there is a next
    • Boolean hasprevious () whether there is a previous
    • Object Next () returns the next element
    • Object previous (); Returns the previous element
13. Set FRAME (unique function of vector)
    • A:vector class overview
    • B:vector Class-specific features
      • public void addelement (E obj)
      • Public E elementat (int index)
      • Public enumeration elements ()
    • C: Case Demo

      • The iteration of the vector

        Vector v = new Vector();                //创建集合对象,List的子类v.addElement("a");v.addElement("b");v.addElement("c");v.addElement("d");//Vector迭代Enumeration en = v.elements();          //获取枚举while(en.hasMoreElements()) {           //判断集合中是否有元素    System.out.println(en.nextElement());//获取集合中的元素}
14. Set frame (array of data structure and linked list)
    • A: Array
      • Query Quick Change also fast
      • Delete Slow
    • B: Linked list
      • Slow query, Slow modification
      • Quick and delete
15, the collection framework (list of the three sub-class characteristics)
    • Features of the three subcategories of a:list
    • ArrayList:    底层数据结构是数组,查询快,增删慢。    线程不安全,效率高。Vector:    底层数据结构是数组,查询快,增删慢。    线程安全,效率低。Vector相对ArrayList查询慢(线程安全的)Vector相对LinkedList增删慢(数组结构)LinkedList:    底层数据结构是链表,查询慢,增删快。    线程不安全,效率高。Vector和ArrayList的区别    Vector是线程安全的,效率低    ArrayList是线程不安全的,效率高共同点:都是数组实现的ArrayList和LinkedList的区别    ArrayList底层是数组结果,查询和修改快    LinkedList底层是链表结构的,增和删比较快,查询和修改比较慢共同点:都是线程不安全的
    • B:list has three sons, who do we use? Query multi-use ArrayList additions and deletions LinkedList if all are more ArrayList

Java EE Fundamentals (15)/collections

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.