The final modifier of the ArrayList has a capacity of 10, what exactly happens when the 11th element

Mondo Technology Updated on 2024-01-30

Hello everyone, I'm Xiaomi!Recently, I have found in various interviews that there is a question about J**A that has attracted a lot of attention, and that is: a quiltfinalRetouchedarraylist, the initialization capacity is set to 10, what exactly happens when we try to add the 11th element?Today we will reveal this mysterious interview question together!

Before we get to this question, let's take a look at the basic concepts of the arraylist and final keywords.

arraylist:ArrayList is a member of the J**A collection framework and belongs to one of the implementation classes of the List interface. It is based on a dynamic array implementation that automatically scales capacity as needed. When we add an element, if the current capacity is insufficient, the ArrayList will automatically expand the capacity.

final keyword:In j**a, final is a keyword used to represent immutable properties that cannot be modified once a value is assigned. When final modifies a class, it means that the class cannot be inherited;When a method is modified, it means that the method cannot be overridden by subclasses;When a variable is modified, it means that the variable is a constant.

First, let's create a final-modified arraylist and set the initialization capacity to 10:

The ArrayList constructor is used here, specifying an initial capacity of 10. Due tofinalRetouching, we can no longer bemylistPoint to other objects, but that doesn't implyarraylistThe elements in are immutable. Let's try to add 11 elements to this arraylist and see what happens.

If we hadn't reached the 11th capacity of the arraylist before adding the 10th element, everything would be fine. The arraylist automatically scales by copying the original elements into a larger array and adding new elements at the end of the new array. At this time,finalKeywords don't have an effect on the content of the arraylist because we haven't changed itmylistReferences.

If we add the 11th element, the capacity of the arraylist has reached 10, but we haven't changed itmylist, then the program will still run successfully. BecausefinalKeywords only guarantee that the reference is immutable, and do not limit the content of the object to which the reference is pointed.

However, if we try to modify it after adding the 11th elementmylistwill run into problems. For example:

Due tomylistwas declared asfinal, we can no longer point it to a new arraylist object. This is in the j**a languagefinalRestrictions on keywords.

finalA modified arraylist does not affect the mutability of the elements in the arraylist, but only ensures that the reference is immutable.

If the capacity of the arraylist is not full before the 11th element is added, everything works fine.

If the capacity is full, but no attempt is made to modify the reference, the program will also run normally.

Trying to modify the reference after adding the 11th element will result in a compilation error becausefinalKeywords limit the reassignment of references.

In practice, we don't usually declare onefinalarraylist and try to modify its reference afterward. Such a design can cause confusion and is not conducive to readability and maintainability. If you need an immutable list, consider using itcollections.unmodifiablelistmethods or other immutable collection classes.

Hopefully, through this article, you'll be interested in j**afinalThe problem of decorating arraylists is clearer. Knowing these details during an interview will not only demonstrate your deep understanding of the J**a language, but will also help you better design and maintain**. If you have more questions about this issue, please leave a message in the comment area, and we will **!Don't forget to like and share Thank you for your support!

If you have any questions or more technical sharing, please follow my WeChat***Know what it is, and know why

Related Pages