Java Road --- Day18 (List collection)

2019-11-05-23:03:28


 

List collection:

  java.util.List interface inherits from the Collection interface, is an important branch of separate collection, habitually will object that implements the List interface called the List collection

  Features:

    1. It is an element of the set of ordered access. For example, the elements are stored sequentially 11,22,33. Then the collection, storage element is in the order of 11, 22, 33 is completed

    2. It is a collection with an index by indexing operation can be accurately set of elements (the index of the array is a reason)

    3. There may be a set of repeated elements by elements equals method to compare whether the repetitive elements

List collection of commonly used methods:

  Collection List as a sub-set of interfaces, not only inherits all the methods of the Collection interface, but also adds some unique method of operation according to the set of indices of elements

    1.public void add (int index, E element): The specified element to a specified location on the set of

    2.public E get (int index): Returns the specified element in the collection position. 

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

    4.public E set (int index, E element): Replace elements in the collection specified location with the specified element, the element returns the value before the update.

. 1  Package demosummary.list;
 2  
. 3  Import of java.util.ArrayList;
 . 4  Import java.util.List;
 . 5  
. 6  public  class ListTest {
 . 7      public  static  void main (String [] args) {
 . 8          // create the collection object List 
. 9          List <String> list = new new the ArrayList <> ();
 10          // to set the additive element list 
. 11          List.add ( "Del Mar" );
 12 is          List.add ( "Nadu" );
 13 is          List.add ( "Prince" );
 14         List.add ( "Juggernaut" );
 15          // print list set 
16          System.out.println (list); // [Del Mar, Nadu, Prince, Juggernaut] 
. 17          / ** 
18 is           public void the Add (int index, E element): the specified element to a specified position on the set
 . 19           * / 
20 is          List.add (. 1, "CASA" );
 21 is          System.out.println (List); // [de Ma, Kasha, Nadu, Prince, Juggernaut] 
22 is          / ** 
23 is           * GET public E (int index): returns the specified element in the collection position.
24           * / 
25          System.out.println (List.get (2)); // Nadu 
26          / ** 
27          * Public E remove (int index) : remove the elements specified in the list position, returns the removed element.
28           * / 
29          System.out.println (list.remove (. 3)); // Prince 
30          System.out.println (List); // [Del Mar, Kasha, Nadu, Juggernaut] 
31          / ** 
32           * E public sET (int index, E element): replace elements in the collection specified location with the specified element, the element returns the value before the update.
33 is           * / 
34 is          String list.set Result = (. 1, "arms" );
 35          System.out.println (Result); // Kasha 
36          System.out.println (List); // [Del Mar, arms, Nadu, Juggernaut] 
37          / * 
38           * set list traversal for enhanced
39          */
40         for (String s : list) {
41             System.out.println(s);
42         }
43     }
44 }

List subclass collection:

  ArrayList Collection:

    java.util.ArrayList set of stored data structure is an array structure. Elemental additions and deletions slow and look for fast, everyday use in the development of multi-functionality to query data through the data, it is commonly used ArrayList collection.

    NOTE: It is free to use ArrayList when many programmers developed any demand, not strict, this use is discouraged. 

  LinkedList collection:

    java.util.LinkedList data storage structure is a set of linked list structure. Convenient element to add, delete collections. (LinkedList is a doubly linked list)

  Some methods of operation LinkeList set (to understand)

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

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

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

    public E getLast (): returns after an element of this list.

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

    public E removeLast (): Removes and returns after an element of this list.

    public E pop (): this list stack represented by the pop-up element.

    public void push (E e): The list element is pushed into the stack represented.

    public boolean isEmpty() :如果列表不包含元素,则返回true

 1 package demosummary.list;
 2 
 3 import java.util.LinkedList;
 4 
 5 public class LinkedListTest {
 6     public static void main(String[] args) {
 7         //创建LinkedList集合对象
 8         LinkedList<String> list = new LinkedList<>();
 9         /**
10          * public void addFirst(E e) :将指定元素插入此列表的开头。
11          * public void addLast(E e) :将指定元素添加到此列表的结尾。
12          */
13         list.addFirst("德玛");
14         System.out.println(list);//[德玛]
15         list.addFirst("德邦");
16         System.out.println(list);//[德邦, 德玛]
17         list.addLast("皇子");
18         System.out.println(list);//[德邦, 德玛, 皇子]
19         /**
20          * public E getFirst() :返回此列表的第一个元素。
21          * public E getLast() :返回此列表的后一个元素。
22          */
23         System.out.println(list.getFirst());//德邦
24         System.out.println(list.getLast());//皇子
25         /**
26          * public E removeFirst() :移除并返回此列表的第一个元素。
27          * public E removeLast() :移除并返回此列表的最后一个元素
28          */
29 //        String s = list.removeFirst();
30 //        System.out.println(s);//德邦
31 //        System.out.println(list);//[德玛, 皇子]
32 //        String s1 = list.removeLast();
33 //        System.out.println(s1);//皇子
34 //        System.out.println(list);//[德玛]
35         /**
36          * public E pop() :从此列表所表示的堆栈处弹出一个元素。
37          */
38         String pop = list.pop();
39         System.out.println(pop);//德邦
40         /**
41          * public void push(E e) :将元素推入此列表所表示的堆栈。
42          */
43         list.push("卡莎");
44         System.out.println(list);//[卡莎, 德玛, 皇子]
45         list.push("武器");
46         System.out.println(list);//[武器, 卡莎, 德玛, 皇子]
47         /**
48          * public boolean isEmpty() :如果列表不包含元素,则返回true
49          */
50         boolean empty = list.isEmpty();
51         System.out.println(empty);//false
52     }
53 }

 

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11802595.html