Differences between arraylist and array-encapsulated containers and native containers

Source: Internet
Author: User

Arraylist and array are the containers we often use in actual programming. Because arraylist is equivalent to a dynamic array, there are too many similarities between them, so that we may be confused when choosing which one to store elements.

So what are the differences between the two and their applicability?

1. stored content

Array, which must store elements of the same type, and arraylist is not necessarily because it can store objects, but the array can also be an object array, and then extract the desired type, in this way, information is not lost (Objects cannot be converted to basic types, but all basic types have corresponding packaging classes). Besides, the arrays encapsulated in arraylist itself are object arrays. Therefore, it is essentially an object array.

Advantages and disadvantages of arraylist

The biggest advantage of using arralist is convenience, because we can use its method for operations, but because arraylist is an internal container that encapsulates the array, there are many of its methods, for example, indexof and contains are all simpleAlgorithmTraversal in the internal array will affect the efficiency (but the impact of this part is really not too concerned ).

The biggest disadvantage of using arraylist is that when we add new elements, it first checks whether the internal array capacity is sufficient. If not, it creates a new array with twice the size of the original array, copies all elements to the new array, and then discards the old array. This is really troublesome, because every time we do this, especially when there are enough elements, this is really a problem that affects both memory and efficiency, however, by testing their running time separately, we find that the biggest impact is that if there are otherCodeYou also need to use the memory, so the array remains unchanged, but the arraylist will become much slower. In the same case, the time spent is more than four times that of the array (the actual situation is far more ).

Mutual conversion between three arraylist and array

Arraylist is an array internally, so it can be converted to an array. The method is as follows:

 
List <integer> List =NewArraylist <integer>(); Integer [] marray= List. toarray (NewInteger [list. getsize ()]);

In this way, the corresponding list can be converted into an array. Our toarray () has a method without parameters, but you must note that this method is used, because our toarray () actually returns an object array, so if it is called like this:

Integer [] marray = (integer) List. toarray ();

An error occurs. The preceding toarray () parameter can be used because its internal implementation returns an array of the corresponding type based on the element type of the passed array, however, we can forcibly convert each element in marray to the corresponding type. Therefore, from the convenience of use, I prefer toarray () with parameters, and the above is a usage. We will input a corresponding array with the length of list in the parameter, returns an array of the desired length.

4. Application scenarios

The conventional declaration method using list is as follows:

 
List <integer> List =NewArraylist <integer> ();

although we want to actually use arraylist rather than list, we know that the parent class can obtain the reference of the subclass and use the subclass method, so we can use the list and arraylist methods at the same time without fear of errors. How can we use these differences? First, an important constraint is that the declared area of list is generally in the main method (of course, static list can also be, but we generally use it as a temporary container for storing elements), while array can be declared externally. In this case, it is a global array. Therefore, their usage is different. If you want to save data that exists and remains unchanged during the entire Program operation, we can put them into a global array, but if we simply want to save the data in the form of an array for us to search, then we will select arraylist. Another thing we must know is that if we need to move or delete elements frequently or process a large amount of data, the use of arraylist is really not a good choice, because it is very inefficient, it is very troublesome to use Arrays for such actions, so we can consider selecting the sort list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.