Common operations of ArrayList and LinkedList

Keywords: Spark Java

The main contents of this paper are as follows:
1. Common operations of ArrayList
2. A Brief Introduction to Random Numbers
3. Comparisons of Java Custom Equivalence
4. A Brief Introduction to Iterators
5. The relationship between Iterable and foreach
6. Common operations of LinkedList
7. A brief description of instanceOf
8.ArrayList exercise (shuffling)
9.LinkedList Common Operational Exercises
1. Common ArrayList operations:
(1) Construction method
ArrayList(); constructs an empty sequential table with default capacity
ArrayList(int capacity); construct an empty sequential table with capacity
ArrayList(Collection c); Construct a sequence table and place all elements in C in the sequence table

List<Integer> origin = Arrays.asList(1,2,3,4,5);
ArrayList<Integer> list = new Arraylist<>(origin);

(2) Common methods:
add(E element): End-interpolation data
add(int index,E element): Insert the element into the index position
remove(Object o): Delete the first O in the sequence table
remove(int index): Remove the index position in the order table
Contains (E): Determine whether e is in a sequence table
IndexOf (E): Returns the first subscript that appears e
LastIndexOf (E): Returns the last subscript that appears e
get(int index): Returns the subscript element
Set (int index, E): Change subscript elements
2. Random Numbers
Under the java.util.Random package:
Random(); and Random(long seed);
NextInt (bound) [0, bound] [bound cannot be 0]
Pseudo-random, in the case of fixed seeds, random generation is fixed. In order to avoid random fixing, time is introduced as seed, Rogue-Like
3. Comparing the Equivalence of Custom Classes in Java
(1) For the reference type=== the judgement is not that the object is equal, but that the reference is equal, and the two references point to the same object.
(2) There is an equals method in the Object class, which is overridden in the custom class to call the equals method of the object.
(3) When customizing objects: toString(), equals() recommend all overrides
4. Iterator
(1) Important methods:
boolean hasNext(): Determine if there are more elements
E next(): Returns the next element in the iteration
void remove(): Delete the last element returned by this iterator
5. The relationship between Iterable and foreach:
foreach: for(int a: container) {};
ListIterator (Bidirectional Iterative Interface): There is previous in the method: you can return the previous element. It is an iterator that can traverse the list in one direction, modify the list during iteration, and get the current position of the iterator in the list.
As long as the class implements the Iterable interface, it can be used by foreach grammar
6.LinkedList
(1) LinkedList implements List/Deque, Deque (double-ended queue, data structure with queue and stack properties)
(2) Construction method: LinkedList();
LinkedList(Collection c);
(3) Method: void addFirst (E): Insert the specified element at the beginning of the list.
Void addLast (E): Insert the specified element at the end of the list
E getFirst(): Returns the first element of the list
E getLast(): Returns the last element in the list
E peek(): Retrieve but not delete the head of the list
E peekFirst(): Retrieves but does not delete the first element of the list
E peekLast(): Retrieves but does not delete the last element of the list
E poll(): Retrieve and delete the header of the list
E pollFirst(): Retrieves and deletes the first element of the list
E pollLast(): Retrieve and delete the last element of the list
Void push (E): Push this element onto the stack represented by the list
E pop(): An element pops up from the stack in this list
List < E > SubList (int from Index, int to Index): Returns from Index (including) and before toIndex
view
7.instanceOf
Whether a instanceOf b:a object can be referred to by a reference to B data type, that is, whether a object type is a small type of B type.
8.ArrayList exercise (shuffling)

//First, define a porkCard class
public class PorkCard {
    private int value;
    private String color;
    public PorkCard(){
    }
    public PorkCard(String color,int value){
        this.color = color;
        this.value = value;
    }
    public int getValue(){
        return value;
    }
    public void setValue(int value){
        this.value = value;
    }
    public String getColor(){
        return color;
    }
    public void setColor(String color){
        this.color = color;
    }
    @Override
    public String toString(){
        return "["+this.color+","+this.value+"]";
    }
    @Override
    public boolean equals(Object o){
        if(o==null){
            return false;
        }
        //Does a reference of PorkCard type point to an o object?
        if(!(o instanceof PorkCard)){
            return false;
        }
        PorkCard porkCard = (PorkCard) o;
        return this.value==porkCard.value&&this.color.equals(porkCard.color);
    }
}
//Secondly, the playCards class is defined to complete each operation.
public class PlayCards {
    public static void temp(ArrayList<PorkCard> arrayList,int i,int j){
        PorkCard temp1 = arrayList.get(i);
        PorkCard temp2 = arrayList.get(j);
        arrayList.set(i,temp2);
        arrayList.set(j,temp1);
    }
    public static void main(String[] args) {
        //Write 52 cards
        ArrayList<PorkCard> arrayList = new ArrayList<>(52);
        //Define an array to put in various colors
        String[] cardColor = {"Heart","Spade","block","Plum blossom"};
        //Four of the 52 cards are in different colors, 13 in each.
        for(int i = 0;i < 4;i++){
            for(int j = 0;j < 13;j++){
                PorkCard porkCard = new PorkCard(cardColor[i],j);
                arrayList.add(porkCard);
            }
        }
        System.out.println(arrayList);
        System.out.println("--------------------------------------");
        //Shuffle - > Remove the 52nd card. After the first 51 cards are randomly selected, two cards are exchanged (except the first card).
        Random random = new Random(20190820);
        for(int i = 51;i >0;i--){
            //i = 51, bound range [0,51)
            int num = random.nextInt(i);
            temp(arrayList,i,num);
        }
        System.out.println(arrayList);
        System.out.println("--------------------------------------");
        //Give five cards to three people.
        ArrayList<PorkCard> A = new ArrayList<>(5);
        ArrayList<PorkCard> B = new ArrayList<>(5);
        ArrayList<PorkCard> C = new ArrayList<>(5);
        for(int i = 1;i<16;i++){
            PorkCard porkCard = arrayList.get(arrayList.size()-1);
            switch (i%3){
                case 0:
                    A.add(porkCard);
                    break;
                case 1:
                    B.add(porkCard);
                    break;
                case 2:
                    C.add(porkCard);
                    break;
                default:
                    break;
            }
            arrayList.remove(porkCard);
        }
        System.out.println(A);
        System.out.println(B);
        System.out.println(C);
        System.out.println("------------------------------------");
        //Check if there is Plum Blossom 4 in A, delete if there is, and return if not.
        PorkCard porkCard = new PorkCard("Plum blossom",4);
        System.out.println(A);
        for(int i = 0;i<A.size();i++){
            if(porkCard.equals(A.get(i))){
                A.remove(porkCard);
                System.out.println(A);
            }
        }
        System.out.println("------------------------------------");
        Iterator<PorkCard> iterator = A.iterator();
        System.out.println(A);
        PorkCard porkCard1 = new PorkCard("Heart",4);
        while (iterator.hasNext()){
            PorkCard porkCard2 = iterator.next();
            if(porkCard1.equals(porkCard2)){
                iterator.remove();
                //Using remove() provided by sets in iterators and foreach loops will cause exceptions, while Iterator.remove() will not.
            }
        }
        System.out.println(A);
        System.out.println("------------------------------------");
        if (A.contains(porkCard1)) {
            System.out.println("Contain");
        } else {
            System.out.println("Not included");
        }
        System.out.println("--------------------------------------");
        ListIterator<PorkCard> listIterator = A.listIterator();
        System.out.println(listIterator.next());
        System.out.println(listIterator.next());
        System.out.println(listIterator.previous());
        System.out.println(listIterator.previous());
    }
}

Operation results:

[[Red Peach, 0], [Red Peach, 0], [Red Peach, 1], [Red Peach, 2], [Red Peach, 3], [Red Peach, 4], [Red Peach, 5], [Red Peach, 6], [Red Peach, 7], [Red Peach, 8], [Red Peach, 9], [Red Peach, 10], [Red Peach, 11], [Red Peach, 11], [Red Peach, 12], [Spark, 0], [Spark, 0]]],, [Spark, 0]],, [Spark, 1], [Spark, 2], [Spark, 3], [spark, 3], [spark, 3]], [4], [spaspades, spa5], [Spades, 6], [Spades, 7], [Spades, 8], [Spades, 9], [Spades, 10], [Black] [spades, 11], [spades, 12], [block, 0], [block, 1], [block, 2], [block, 2], [block, 3], [block, 4], [block, 5], [block, 6], [block, 4], [block, 7], [block, 8], [block, 9], [block, 9], [block, 10], [block, 11], [block, 11], [block, 12], [plum flower, 0],,], [plum flower, 0], [plum flower, 1], [plum flower, 2], [plum flower, 3], [blosso, 3], [plum, 4], [plum flower, 4], [plum flower, 4], [plum flower, 5], [plum flower[Plum blossom, 7], [Plum blossom, 8], [Plum blossom, 9] [Plum blossom, 10], [Plum blossom, 11], [Plum blossom, 12]]
--------------------------------------
[box, 1], [box, 1], [box, 7], [box, 3], [spades, 12], [spades, 8], [plum, 5], [spark, 8], [plum, 7], [red fruit, 7], [red fruit, red fruit, 5], [red fruit, 5], [spark, 10], [plum, 1], [red, 12], [spark, 12], [plum, 12], [block, 8], [spark, 6]], [plum, 6]], [plum, 11], [11], [spark, 8], [spark, 2], [spark, 2]], [spark, 2]], [box]], [9], [square]][Plum blossom, 3], [heartwood, 6], [cube, 12], [heartwood, 3],[[ [Plum, 2], [Plum, 2], [Plum, 10], [Block, 6], [Block, 6], [Block, 11], [Block, 4], [Red Peach, 9], [Red Peach, 1], [Spafruit, 10], [spades, spades, 1], [spades, 7], [Plum, 6], [Red, 0], [spark, 0], [spades, 4], [Plum, 0], [spark, 0], [spark, 3]], [Plum, 3], [Plum, 9], [Block, 9], [Block, 10], [Spaspaspaspade, 5], [11], [spaspades, 11], [11]Spades, 1], [cubes, 5], [hearts, 4], [hearts, 2], [spades] 9], [Plum blossom, 4], [Plum blossom, 7], [cube, 2]]
--------------------------------------
[[Plum Blossom, 4], [Red Peach, 4], [Plum Blossom, 8], [Block, 10], [Plum Blossom, 0]]
[[cubes, 2], [spades, 9], [cubes, 5], [hearts, 11], [plum blossoms, 9]]
[[Plum Blossom, 7], [Red Peach, 2], [Spades, 1], [Spades, 5], [Spades, 3]]
------------------------------------
[[Plum Blossom, 4], [Red Peach, 4], [Plum Blossom, 8], [Block, 10], [Plum Blossom, 0]]
[[Red Peach, 4], [Plum Blossom, 8], [Block, 10], [Plum Blossom, 0]]
------------------------------------
[[Red Peach, 4], [Plum Blossom, 8], [Block, 10], [Plum Blossom, 0]]
[[Plum blossom, 8], [cube, 10], [Plum blossom, 0]]
------------------------------------
Not included
--------------------------------------
[Plum blossom, 8]
[Block, 10]
[Block, 10]
[Plum blossom, 8]

9.LinkedList Common Operational Exercises

public class MyLinkedList {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);
        System.out.print(list.peek()+" ");
        System.out.print(list.peekFirst()+" ");
        System.out.println(list.peekLast());
        System.out.print(list.poll()+" ");
        System.out.print(list.pollFirst()+" ");
        System.out.println(list.pollLast());
        System.out.println(list);
        list.push(1);
        list.push(2);
        list.push(3);
        System.out.println(list);
        System.out.println(list.pop());
    }
}

Operation results:

[1, 2, 3]
1 1 3
1 2 3
[]
[3, 2, 1]
3

Posted by Altec on Tue, 20 Aug 2019 04:58:28 -0700