Java Road -- Day18(List set)

2019-11-05-23:03:28

 

List collection:

java.util.List interface inherits from Collection interface, which is an important branch of single column Collection. It is customary to call objects that implement List interface List Collection

Features:

1. It is a collection of elements with orderly access. For example, the order in which the elements are stored is 11, 22, 33. In the collection, the storage of elements is completed in the order of 11, 22 and 33

2. It is a collection with index, through which the elements in the collection can be operated accurately (the same principle as the index of array)

3. There can be duplicate elements in the collection. Use the equals method of the element to compare whether it is a duplicate element

Common methods for List collection:

As a sub interface of Collection collection, List not only inherits all the methods in Collection interface, but also adds some unique methods to operate Collection according to element index

1.public void add (int index, E element): add the specified element to the specified location in the collection

2.public E get(int index): returns the element at the specified location in the collection.  

3.public E remove(int index): remove the element at the specified position in the list, and return the removed element.  

4.public E set(int index, E element): replace the element at the specified location in the set with the specified element, and return the element before the value is updated.

 1 package demosummary.list;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class ListTest {
 7     public static void main(String[] args) {
 8         //Establish List Aggregate object
 9         List<String> list = new ArrayList<>();
10         //towards list Collection add element
11         list.add("DMAR");
12         list.add("Debon");
13         list.add("Prince");
14         list.add("Blademaster");
15         //Printing list aggregate
16         System.out.println(list);//[DMAR, Debon, Prince, Blademaster]
17         /**
18          public void add(int index, E element) : Adds the specified element to the specified location in the collection
19          */
20         list.add(1,"casa");
21         System.out.println(list);//[DMAR, casa, Debon, Prince, Blademaster]
22         /**
23          *public E get(int index) :Returns the element at the specified location in the collection.
24          */
25         System.out.println(list.get(2));//Debon
26         /**
27          * public E remove(int index) : Remove the element at the specified position in the list, and return the removed element.
28          */
29         System.out.println(list.remove(3));//Prince
30         System.out.println(list);//[DMAR, casa, Debon, Blademaster]
31         /**
32          * public E set(int index, E element) :Replaces the element at the specified location in the collection with the specified element, returning the element before the value is updated.
33          */
34         String result = list.set(1, "arms");
35         System.out.println(result);//casa
36         System.out.println(list);//[DMAR, arms, Debon, Blademaster]
37         /**
38          * Enhance for traversal list collection
39          */
40         for (String s : list) {
41             System.out.println(s);
42         }
43     }
44 }

Subclass of List collection:

ArrayList collection:

The structure of the java.util.ArrayList collection data store is an array structure. Element addition and deletion are slow, and search is fast. Because many functions are used in daily development to query and traverse data, ArrayList is a common collection.

Note: many programmers use ArrayList to complete any requirements at will, which is not rigorous, and this usage is not advocated.  

LinkedList collection:

The structure of java.util.LinkedList collection data store is linked list structure. A collection for adding and deleting convenience elements. (LinkedList is a two-way linked list)

Some operation methods of LinkeList collection

public void addFirst(E e): inserts the specified element at the beginning of this list.

public void addLast(E e): adds the specified element to the end of this list.

public E getFirst(): returns the first element of this list.

public E getLast(): returns the next element of this list.

public E removeFirst(): removes and returns the first element of this list.

public E removeLast(): removes and returns the next element of this list.

public E pop(): pops an element from the stack represented by this list.

Public void push (e e e): push elements into the stack represented by this list.

public boolean isEmpty(): returns true if the list does not contain elements

 1 package demosummary.list;
 2 
 3 import java.util.LinkedList;
 4 
 5 public class LinkedListTest {
 6     public static void main(String[] args) {
 7         //Establish LinkedList Aggregate object
 8         LinkedList<String> list = new LinkedList<>();
 9         /**
10          * public void addFirst(E e) :Inserts the specified element at the beginning of this list.
11          * public void addLast(E e) :Adds the specified element to the end of this list.
12          */
13         list.addFirst("DMAR");
14         System.out.println(list);//[DMAR]
15         list.addFirst("Debon");
16         System.out.println(list);//[Debon, DMAR]
17         list.addLast("Prince");
18         System.out.println(list);//[Debon, DMAR, Prince]
19         /**
20          * public E getFirst() :Returns the first element of this list.
21          * public E getLast() :Returns the next element of this list.
22          */
23         System.out.println(list.getFirst());//Debon
24         System.out.println(list.getLast());//Prince
25         /**
26          * public E removeFirst() :Remove and return the first element of this list.
27          * public E removeLast() :Remove and return the last element of this list
28          */
29 //        String s = list.removeFirst();
30 //        System.out.println(s);//Debon
31 //        System.out.println(list);//[DMAR, Prince]
32 //        String s1 = list.removeLast();
33 //        System.out.println(s1);//Prince
34 //        System.out.println(list);//[DMAR]
35         /**
36          * public E pop() :Pop an element from the stack represented by this list.
37          */
38         String pop = list.pop();
39         System.out.println(pop);//Debon
40         /**
41          * public void push(E e) :Push elements onto the stack represented by this list.
42          */
43         list.push("casa");
44         System.out.println(list);//[casa, DMAR, Prince]
45         list.push("arms");
46         System.out.println(list);//[arms, casa, DMAR, Prince]
47         /**
48          * public boolean isEmpty() : Returns true if the list does not contain elements
49          */
50         boolean empty = list.isEmpty();
51         System.out.println(empty);//false
52     }
53 }

Tags: Java

Posted on Tue, 05 Nov 2019 11:55:34 -0800 by Aliz