Collections framework

Java.lang. Iterable ← Java.util. Collection interface

There are also List and Set interfaces under Collection interface.

The List interface can store a non-unique, ordered set of objects (which can be NULL); Common implementation classes are ArrayList and LinkedList.

The ArrayList collection class

  1. Difference with array:

    The array is encapsulated to realize the dynamic array with variable length and any data type can be added. The added data will be converted into Object type.

    Once an array is defined to be immutable, only one data type can be added.

    Similarities: are used in memory allocation of contiguous space

  2. Commonly used method

    methods instructions
    boolean add(Object o) Add the element O to the end of the list, starting at 0
    void add(int index,Object o) Adds element O at the specified index position, which must be between 0 and the number of elements in the list
    int size() Returns the number of elements in the list
    Object get(int index) Returns the element at the specified index position. The retrieved element is of type Object and needs to be cast before use
    void set(int index,Object o) Replace the element at the I index position with the o element
    boolean contains(Object o) Checks whether the specified element o exists in the list
    int indexOf(Object o) Returns the index position where the o element appears in the collection
    boolean remove(Object o) Removes element O from the list
    Object remove(int index) Removes the element from the list at the specified position, starting at 0

2, LinkedList

  1. Data is stored in linked list storage mode

  2. The advantages and disadvantages:

    • The advantage is that the efficiency of inserting and deleting elements is high
    • The disadvantage is that the efficiency of the search is very low
  3. Supports the implementation of all optional lists in the List interface, and allows element values to be any data, including NULL

3. Iterator interface

  1. The Iterator interface represents an Iterator that iterates over a collection of heaps

  2. Because it inherits iterable indirectly, it can also be traversed with foreach

  3. Common methods:

    • HasNext () : Determines whether the next accessible element exists and returns true if there are still elements to iterate over
    • Next () : Returns the next element to access
  4. Traversal code:

    ArrayList list = new ArrayList;
    list.add("Zhang");
    list.add("Two");
    list.add("Mike");
    Iterator it = list.iterator();
    while(it.hastNext()){
        System.out.println(it.next());
        //String s = (String)it.next(); Change from Object to String
        //System.out.println(s);
    }
    Copy the code

Yeah,

1. Only modifiers can be called:

Public, package, private, protected, abstract, final, static

2. Why have get/set methods?

Because you can use get/set method to judge whether the input data is reasonable, reject unreasonable data

3,String s = new Student(); What did you do?

  1. new Student()A space is created in the heap
  2. The size of this space is determined by the class’s member variables:
    • The String type is a reference type, and the space stored in the address, address is about int, all open 4 bytes of space
    • The int type takes up four bytes; Char takes up 2 bytes of space
    • At least 4+4+2=10 bytes of space
  3. When space is available, the JVM gives a default value of 0
    • The default value for String is nullSystem.identyhashcode (reference variable), the output is 0;
      • System.identyhashcode (reference variable)Gets the value returned by the hachCode method provided by Object
    • Int Default is 0;
    • Char defaults to a blank (\u000), not a space. In Eclipse, the output of a char that is 0 is a square, but in other compilers it may be blank.
    • Boolean defaults to false, which is also 0(true = 1, false = 0); It takes up only 1 binary in memory, but the smallest unit of disk is 1 byte.
  4. ({}, static{})
  5. The constructor is then executed

6. Constructional methods are related

  • Constructors and non-static modifications are instances
  • The new + constructor together returns the first address of a new space, which is given to a variable, soString s = new Student()In actually,new Student()S is just a variable that receives this object.
  • s.setName("Amy")//main() does not return the name attribute;
  • this.name = Amy// My (.) name is (=) Amy