[Java-Data Structure] Specifying the size of the ArrayList array is conducive to data expansion and time-consuming reduction

The essential

"Because the expansion operation involves memory application and data movement, it is time-consuming. Therefore, if the size of the data to be stored can be determined in advance, it is best to ArrayListspecify the data size in advance when creating."

As shown in the following code:

ArrayList<User> users = new ArrayList(10000);
for (int i = 0; i < 10000; ++i) {
    
      
	users.add(xxx);
}

insert image description here
Then it means that if the amount of data is greater than 10, it is best to set the required amount of data in advance.

Guess you like

Origin blog.csdn.net/weixin_44002043/article/details/131535131