"Tangled" ambiguous ArrayList and Array

Foreword

One array can be automatically reduced out certain elements like the delete. But where did the words of Java? Array elements can delete it? Today to talk about a special object --ArrayList, it appears with the presence and the array has something similar. Here in accordance with what it is - what is the use - in terms of how to use? Then talk about the difference between an array?

Positive word (personal opinion, is incorrect please advise)

Convention to understand what it is?

  • Source out on! ! ! Obviously it is a class, inheritance (extends) the AbstractList, implement (implements) the List, RandomAcess and so on. ( The extends can be understood as the overall function .implements inherited parent class for this class can be understood as an additional some additional features ). It is a capacity that grows automatically dynamic array (not the array), can support the delete operation.

  • image.png

  • image.png

  • 10 to its initial capacity. The following is a first constructor configured capacity of 10. To change this capacity, like a second constructor that on it. The third element is configured to specify a collection comprising a list, in the order they are returned by the iterator is set.

image.png
  • It is worth mentioning that, Java Collections Framework interfaces are defined in the List, and only two to achieve, in addition to this there ArrayList LinkedList. Has such a two conclusions on the data structure and algorithm analysis of the book, specifically in the book it is to get through the algorithm comes from. LinkedList actually use doubly-linked list in Java and C #.

    • ArrayList advantage is added at the end, delete, get and set (set and get).
    • LinkedList advantage is beginning to add class to delete elements from the start.
  • Table array (the ArrayList) and very similar to that array, the read time is O (1), the insertion time becomes O (n), the table except that this may increase the length of the array, when added, just footer number plus one will be able to determine the new element.

  • ArrayList is not thread safe. It said a security thread is completely object can be used simultaneously by multiple threads, no problem. This often encountered in-depth study of virtual machines back - synchronous or synchronized using the lock (lock) can be resolved between the use of multi-threading.

So what does it do with it?

Dynamically increase and decrease the elements to achieve a list and ICollection interfaces. Of course, also have the flexibility to select the size of the array.

  1. Delete action can be achieved in the array can not be deleted

    image.png
  2. Was added to achieve the overall capacity, add elements to the array table

    image.png

3. The common method

image.png

how to use

  1. I want to store a number, which is the basic data type packaging can be used (under the java.lang).

    • basic type Wrapper class
      int Integer
      byte Byte
      short Short
      long Long
      float Float
      doublr Double
      char Character
      boolean Boolean
    • ArrayList<Integer> list = new ArrayList<>;//这样创建不会报错了
  2. Example: generating a random number between 1-30, added to the collection, storage 5

    • //主代码
      ArrayList<Integer> list2= new ArrayList();
      Random r = new Random();
      for(int i = 0; i < 5; i++){
          int num = r.nextInt(30) + 1;
          list.add(num);
      }

Guess you like

Origin www.cnblogs.com/yhycoder/p/12044110.html