Java learning DAY16 list, iterator

List

Implementation classes: ArrayList, LinkedList, Vector, Stack

***ArrayList: ***The bottom layer implements data storage by an array. The default initial capacity is 10. The bottom layer of the array is expanded by half according to the right shift operation.

***ArrayList (sequence list)***: The bottom layer is the data implemented by the array, the default initial capacity is 10, the memory is continuous, the bottom layer needs to be expanded according to the right shift operation, and each expansion is based on the original capacity. half. The addition and deletion efficiency is low, and the query efficiency is high. A thread-unsafe collection.
Small Exercise: Implementing ArrayList with Arrays

package cn.tedu.collection.list;

import java.util.ArrayList;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        //Create a collection object
        //Objects that are shaped up
        List<String> list=new ArrayList<>();
        //add element
        list.add("abc");
        list.add("bc");
        list.add("ac");
        list.add("ab");
        //insert element
        //The maximum supported insertion subscript is the number of elements
        //IndexOutOfBoundsException---Subscript out of bounds exception
        //list.add(5,"123");

        //Remove elements by subscript
        //The maximum supported deletion subscript is the number of elements minus 1
        //list.remove(2);

        //Remove elements based on specified content
        //Do nothing if the specified content does not appear in the element
        list.remove("a1b");

        //empty collection
        //list.clear();

        //Determines whether the specified content is contained in the element
        //System.out.println(list.contains("ac"));

        //Get the element according to the specified subscript
        //System.out.println(list.get(0));

        //Returns the subscript value of the first occurrence of the specified content
        //System.out.println(list.indexOf("ab"));

        //Check if it is an empty set
        //System.out.println(list.isEmpty());

        //replace element
        /*list.remove(0);
        list.add(0,"123");*/
        //list.set(0,"123");

        //returns the number of elements
        //System.out.println(list.size());

        //Truncate sublist
        //Specifies that the subscript includes the head but not the tail
        //System.out.println(list.subList(1,3));

        //Put the elements of the collection into an array to return
        /*Object[] os=list.toArray();
        for (Object o:os) {
            String s=(String) o;
        }*/
        //Pass in an array object and return an array of the corresponding type
        //The length of the given array only needs to be greater than or equal to zero
        String[] ss=list.toArray(new String[1]);


        //Ensure that data is stored in an orderly manner
        System.out.println(list);
    }
}

LinkedList (linked list)
The bottom layer stores data based on nodes (static inner classes), and maintains nodes by pointing to address values. The memory is not continuous and does not need to be expanded. The addition and deletion efficiency is high, and the query efficiency is low. A thread-unsafe collection.
**Thinking:** When adding and deleting operations in the scene are equivalent to query operations, should you choose ArrayList or LinkedList?

Vector:
earliest collection class
The bottom layer implements data storage based on arrays, and the default initial capacity is 10
The bottom layer is expanded according to the ternary operator. If the increment value is greater than 0, the value of each expansion is the incremental value, and if the increment is not greater than 0, the value of each expansion is the size of the original capacity.
Thread-safe collections

package cn.tedu.collection.list;

import java.util.Vector;

public class VectorDemo {
    public static void main(String[] args) {
        //If the specified value is expanded, the value of each expansion is the incremental value
        //If you do not specify an increment, each expansion is doubled on the original basis.
        Vector<String> vector=new Vector<>(10,5);
        //add element
        for (int i = 0; i < 84; i++) {
            vector.add("a");
        }
        //Returns the current capacity value
        System.out.println(vector.capacity());
    }
}

Stack (stack structure):
first in last out
The top element of the stack: the last stored element
Bottom element of the stack: the first stored element
Push/Push: Store elements on the stack
Pop/pop: get elements from the stack
Inherit the Vector class

package cn.tedu.collection.list;

import java.util.Stack;

public class StackDemo {
    public static void main(String[] args) {
        //Create stack object
        Stack<String> s=new Stack<>();
        //push onto the stack
        s.push("abc");
        s.push("ab");
        s.push("bc");
        s.push("ac");
        //Returns the top element of the stack without removing it
        //If it is an empty stack---EmptyStackException (empty stack exception)
        //System.out.println(s.peek());
        //Return the top element of the stack and delete it
        //System.out.println(s.pop());
        //Check if the stack is empty
        System.out.println(s.empty());
        //Find the specified content from the top of the stack to the bottom of the stack to find the position of the specified content (counting from 1)
        //return -1 not found
        System.out.println(s.search("ac"));
        //Ensure consistency with the characteristics of the List interface
        //Ensure that data is stored in an orderly manner
        System.out.println(s);
    }
}

Iterator (Iterator interface)

The bottom layer moves to iterative traversal according to the pointer
You cannot directly add or delete elements of the original collection during traversal
The Itrator method is defined in the Itrator interface
Objects generated by classes that implement the Itrator interface can be manipulated by enhanced for loops - the bottom layer of enhanced for loops is implemented by iterators - enhanced for unreal is a new feature of jdk1.5

package cn.tedu.collection.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorDemo {
    public static void main(String[] args) {
        //Create a collection object
        List<String> list=new ArrayList<>();
        //add element
        list.add("abc");
        list.add("bc");
        list.add("ab");
        list.add("ac");
        //Calling a method returns an iterator
        Iterator<String> it=list.iterator();
        //Implemented by loop
        while (it.hasNext()){//Determine if the next bit has an element
            //get the next element
            String s=it.next();
            //System.out.println(s);
            //The remove method provided by the iterator is called
            //Changing the tag value indirectly deletes the original collection element
            //it.remove();
            //call the remove method of the collection
            //Call the remove method of the collection to directly delete the original collection element, but it needs to be compared with the default tag value
            //At this point it will report an error
            //list.remove(s);
        }
        //output collection object to store data
        System.out.println(list);
    }
}


package cn.tedu.collection.list;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class ComparatorDemo {
    public static void main(String[] args) {
        //Create a collection object
        List<String> list=new ArrayList<>();
        //add element
        list.add("abc");
        list.add("bc");
        list.add("1ab");
        list.add("ac");
        //Call sort to sort the elements of the collection
        //The Comparator interface represents the comparator, you need to specify the comparison rules and don't worry about the others
        list.sort(new Comparator<String>() {
            //Override method --- specify comparison rules
            //Sort by first letter in ascending order
            @Override
            public int compare(String o1, String o2) {
                //If the return value is an integer, the front object is larger than the back object
                //If the return value is an integer, the front object is smaller than the back object
                //If the return value is 0, the front object is equal to the back object
                return o2.charAt(0)-o1.charAt(0);
            }
        });
        System.out.println(list);
    }
}

Tags: Java linked list api list

Posted by rmbarnes82 on Fri, 20 May 2022 19:51:40 +0300