javaSE___ Collective system 03____LIst set, iterator

1, Overview and test of unique functions of List collection

1. void add(int index,E element) adds an element at the specified position. The test is as follows:

public static void main(String[] args) {

        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add(4, "f");						//Index < = size and index > = 0 will not report exceptions
        System.out.println(list);           //Output [a, b, c, d, f]
    }

2. E remove(int index) deletes the specified element through the index. The test is as follows:

public static void main(String[] args) {

        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        Object obj = list.remove(1);				//Delete the element through the index and return the deleted element
        System.out.println(obj);        //b
        System.out.println(list);       //[a, c, d]
    }

3. E get(int index) obtains elements through index. The test is as follows:

public static void main(String[] args) {

        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        Object obj1 = list.get(2);
        System.out.println(obj1);           //c
    }

Traversing the List collection by index

public static void main(String[] args) {

        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        //Traversing the List collection by index
        for(int i = 0;i < list.size(); i++) {
            System.out.println(list.get(i));	//Output abcd
        }
    }

4. E set(int index,E element) modifies the value of the specified index element position. The test is as follows:

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");						//Modify the element at the specified position
        System.out.println(list);       //[a, z, c, d]
    }

2, The List collection stores the student object and traverses it

Use toString in Student entity class

public static void main(String[] args) {
		List list = new ArrayList();
		list.add(new Student("Zhang San", 23));			//Object obj = new Student("Zhang San", 23);
		list.add(new Student("Li Si", 24));
		list.add(new Student("Wang Wu", 25));
		list.add(new Student("Zhao Liu", 26));
		
		for(int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));	 			//Get each element through the index and output the object added above
		}
	}

Using the get method

public static void main(String[] args) {
		List list = new ArrayList();
		list.add(new Student("Zhang San", 23));		//Object obj = new Student("Zhang San", 23);
		list.add(new Student("Li Si", 24));
		list.add(new Student("Wang Wu", 25));
		list.add(new Student("Zhao Liu", 26));
		
		for(int i = 0; i < list.size(); i++) {
			Student s = (Student)list.get(i);
			System.out.println(s.getName() + "..." + s.getAge());	//Output the object added above
		}
	}

3, Causes and solutions of concurrent modification exceptions. Elements are added while traversing, which is called concurrent modification.

1. Let's take an example

Requirements: I have a collection. I want to judge whether there is a "world" element in it. If so, I will add a "javaee" element. Please write code to implement it.

public static void main(String[] args) {

        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");			//Concurrent modificationexception will be thrown here
            } 
        }
    }

Why throw an exception? Because in the process of traversal, we add elements to it. Adding elements while traversing is called concurrent modification.

Solution: use the list specific iterator ListIterator:

   iterators iterate elements and iterators modify elements (add, a special function of ListIterator), so that concurrent modification exceptions will not be thrown.

public static void main(String[] args) {

        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("world");
        list.add("d");
        list.add("e");

        ListIterator lit = list.listIterator();		//If you want to add elements during traversal, you can use the add method in ListIterator
        while(lit.hasNext()) {
            String str = (String)lit.next();
            if(str.equals("world")) {
                lit.add("javaee");
            }
        }
    }

4, List collection, (ListIterator iterator) (understand)

Main methods:

  • Does boolean hasNext() have a next
  • Does boolean hasPrevious() have a previous one
  • Object next() returns the next element
  • Object previous(); Returns the previous element

5, List sets arrays and linked lists of data structures.

  • A: Array
    • Quick query and quick modification
    • Slow addition and deletion
  • B: Linked list
    • Slow query and slow modification
    • Fast addition and deletion

6, The characteristics of the three subclasses of List.

  • A: Characteristics of three subclasses of list

      ArrayList:
         the underlying data structure is array, which is fast to query and slow to add or delete.
         thread is unsafe and efficient.
      Vector:
         the underlying data structure is array, which is fast to query and slow to add or delete.
         thread safety and low efficiency.
         Vector is slower than ArrayList query (thread safe)
         the addition and deletion of Vector is slower than LinkedList (array structure)
      LinkedList:
         the underlying data structure is linked list, which is slow to query and fast to add and delete.
         thread is unsafe and efficient.

      difference between Vector and ArrayList
         Vector is thread safe and inefficient
         ArrayList is thread unsafe and efficient
       common ground: they are all implemented by arrays
      the difference between ArrayList and LinkedList
         the bottom layer of ArrayList is array results, which is fast to query and modify
         the bottom layer of LinkedList is a linked list structure. It is fast to add and delete, and slow to query and modify
      common ground: threads are unsafe

  • B:List has three sons. Who do we use?
      query multi-purpose ArrayList
      add or delete multi-purpose LinkedList
      if there are many ArrayList s

----

It's not easy to create. If you feel good, please praise it. Thank you~~

Keywords: Java JavaSE set

Added by faizulbari on Tue, 08 Feb 2022 18:37:46 +0200