ArrayList (ArrayList) : ArrayList (ArrayList) : ArrayList (ArrayList) : ArrayList (ArrayList)

ArrayList initialization – Java things column


Again, ArrayList is a generic class, and we can write one ourselves if we like.

After the column was published, the following question was asked in the comments section.

Let’s review the array we talked about before. Without further ado, here’s the code:

The old rule, let’s continue to draw, to deepen the impression, above:

We have removed all the useless details (method areas, constant pools, etc.) from the ArrayList tutorial so that you can see them clearly. Let’s use Eclipse debug to see if they match our image

Let’s look at the execution result, which is also what we expect.

Ok, let’s change the code and add a Person object called “Week Eight” to the array

To perform a

See the legendary array subscript out of bounds exception. In Java, arrays are created in heap memory and have a fixed length.

Since it’s fixed, what if we want to add an “Thursday” user to the array? There’s nothing I can do about it. I’m just going to make a new array, a longer array, and I’m going to copy the elements of the original array, so let’s start writing code, I’m sure you can write code

It’s easy and clean to loop through the elements of the old array and assign to the new array. The debug to look at

“Tuesday” already exists. The above code is simple, but not the most elegant, as an old bird would write it, and the result is the same as the above code.

Let me draw another picture to impress you:

This diagram has exhausted my prehistorical powers, so I want you to think more about how objects look in heap memory in the future. It’s worth my effort. Does the system.arrayCopy () method look familiar? ArrayList (ArrayList) : ArrayList (ArrayList) : ArrayList (ArrayList) : ArrayList (ArrayList) If an ArrayList does not specify a number of constructs, the first time you add an element to it, the underlying array initializes an array of length 10. When the 11th element is added

Look at the grow() method

Here is the code: Int newCapacity = oldCapacity + (oldCapacity >> 1), oldCapacity = oldCapacity + (oldCapacity/2).

The problem that we solved at the beginning of this article is the capacity expansion of the array, which is usually oldCapacity + (oldCapacity >> 1), which is equivalent to 1.5 times the capacity expansion.

When you ask about the difference between an array and an ArrayLIst in a future interview, you’ll probably have a better idea of what’s going on.

ArrayList also provides other constructors, which we’ll look at in passing.

Let’s look at the source code again, it’s very simple:

When we’re writing code, if we know roughly the number of elements, like a class of 40-50 people, we’d prefer List<Person> list2 = new ArrayList<>(50) to be constructed in a specified way to avoid multiple copies of the underlying array. Thereby improving program performance.

Note: This column was first published on the public account saysayJava. All sample codes have been uploaded to the public account, please pay attention to download.


If you liked this series, please give me a thumbs up or share it with me. Your support is what keeps me going, and leave a comment in the comments section where you can find out what you want to know about this column.

Reprint unlimited welcome, but please indicate “author” and “original address”. Please keep this paragraph in the article, thank you for your respect for the author’s copyright. For commercial reprint or publication, please contact the author for authorization.

Previous: ArrayList initialization – Java stuff column

Next: Time Complexity – Java things column