Java SE 040 ArrayList source code in-depth analysis

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 040 ArrayList source code in-depth analysis

1.ArrayList

(1) Array itself receives an Object, so anything can be put in it. Because everything except native data types is Object.

(2) When taking it out, you need to know what type of object you put in.

(3) Only suitable for placing objects in the collection. Cannot place native data types.

package com.javase.arraylist;

import java.util.ArrayList;

public class ArrayListTest {
    
    
	public static void main(String[] args) {
    
    
		ArrayList list = new ArrayList();
		
		list.add(new Integer(3));
		list.add(new Integer(4));
		list.add(new Integer(5));
		list.add(new Integer(6));
		/**
		 * 不能将Object[]转换为Integer[]
		 * 错误代码Integer[] in = list.toArray();
		 */
		Object[] in = list.toArray();
		for(int i = 0 ; i < in.length; i++){
    
    
			System.out.println(((Integer)in[i]).intValue());
		}
	} 
}

2. ArrayList implementation

(1) What is stored in the collection is still the reference of the object rather than the object itself.

(2) The bottom layer of ArrayList is implemented by an array. When an ArrayList object is generated using a construction method without parameters, an Object type array with a length of 10 will actually be generated at the bottom layer.

When the method generates an ArrayList object, it will actually generate an Object type array of length 10 at the bottom.

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108501242