Mục lục bài viết
Update: 2021-12-19 19:19:05,Bạn Cần kiến thức và kỹ năng về ArrayList constructor in Java. Quý quý khách trọn vẹn có thể lại Thảo luận ở cuối bài để Ad được tương hỗ.
This article is part of ArrayList class series. In this article, we will look at 3 different constructors provided in ArrayList class.
Tóm lược đại ý quan trọng trong bài
ArrayList class is a better form of an array. ArrayList is an array, but a resizable one, with a rich set of methods.
A little about ArrayList.
We will look at the working of all these constructors with examples.
public ArrayList()
public ArrayList(int initialCapacity)
public ArrayList(Collection c)
The ArrayLists class hierarchy looks like this :
ArrayList Hierarchy.
This constructor is used to create an ArrayList with no specification for the underlying array. Remember, ArrayList is a smart wrapper on the array itself.
In fact, this constructor is the most used constructor while creating the ArrayList. Let us create an ArrayList of String.
List names = new ArrayList();
names.add(“John”);
names.add(“Jane”);
So in this example we created the ArrayList called names. It will hold String elements. We added two distinct elements called John and Jane.
Internal of ArrayList :
When we add the first element in ArrayList the underlying implementation of ArrayList creates an array of 10 elements.
What happens after we add 10 elements? Well, the underlying implementation will allocate a new array of size roughly 1.5 times the original array and copy all elements from old array to the new one. You dont have to giảm giá with the internal implementation.
This is the most popular constructor as it is simple to use. Let us look at two other constructors.
ArrayList(int initialCapacity) is used to create an ArrayList with underlying array size equal to initialCapacity.
Let us take an example and understand it more.
List names = new ArrayList(20);
names.add(“John”);
names.add(“Jane”);
In this example we created the ArrayList with initialCapacity of 20 elements and added two elements. So the underlying array has a capacity of 20 elements. When we add the 21st element, it will create a new array roughly 1.5 times of the old array size. Once that is done it copies all elements of the old array to the new array.
It may look like a good idea to use this constructor instead of public ArrayList() but we need to be careful. If we choose initialCapacity too low then the ArrayList has to frequently create new arrays to accommodate more values. If you choose initialCapacity too high, then the ArrayList would be too large and it would waste space. We need delicate handling while using this constructor. If in doubt use the public ArrayList() constructor. If you want to use this constructor, select proper initialCapacity after running benchmarks in junit.
public ArrayList(Collection c) constructor acts as a bridge between other collections and ArrayList. Note that the parameter is Collection so any implementation of Collection interface can be input of this constructor.
Assume that you have an instance of HashSet and want to convert that into ArrayList, you can just use this constructor.
If the input Collections is null, the constructor will throw NullPointerException.
Let us take an example :
Set names = new HashSet();
names.add(“Ned”);
names.add(“Catelyn”);
List list = new ArrayList(names);
Assert.assertEquals(2, list.size());
We inserted HashSet as input to the constructor. ArrayList copy all elements from HashSet and insert in ArrayList. The HashSet remains unchanged as part of this call. ArrayList will just read the data from HashSet, it will not modify HashSet.
In this article we saw three different constructors of ArrayList class.
– Một số từ khóa tìm kiếm nhiều : ” Video full hướng dẫn ArrayList constructor in Java tiên tiến và phát triển nhất , Chia Sẻ Link Download ArrayList constructor in Java “.
Bạn trọn vẹn có thể để lại phản hồi nếu gặp yếu tố chưa hiểu nghen.
#ArrayList #constructor #Java