import package that needed only. (#18819)

* import package that needed only.

if the whole package imported, file size increases

* some features of ArrayList

* changed format

* Update index.md
This commit is contained in:
vikash vaibhav
2018-10-16 02:45:38 +05:30
committed by Quincy Larson
parent 335c4b120b
commit c31aa711d6

View File

@ -6,8 +6,14 @@ title: ArrayList
The *Collection framework* consists of all interfaces and classes that can hold a set of values (similar to [arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)). **ArrayList** is a class that is in this hierarchy and known as a _**Collection object**_. It implements the *List* interface which in turn implements the *Collection* interface. This *Collection* interface can be found in the `java.util` package. You will need to import this package. The *Collection framework* consists of all interfaces and classes that can hold a set of values (similar to [arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)). **ArrayList** is a class that is in this hierarchy and known as a _**Collection object**_. It implements the *List* interface which in turn implements the *Collection* interface. This *Collection* interface can be found in the `java.util` package. You will need to import this package.
import java.util.ArrayList; //it would more efficient.
always import specific package that saves memory size and works in less time
ArrayList is a class that is used to create dynamic arrays. It is slower than regular arrays but allows for a lot of manipulation. It can be initialized to have a specific size or it will have a default size of 10 units. ArrayList is a class that is used to create dynamic arrays. It is slower than regular arrays but allows for a lot of manipulation. It can be initialized to have a specific size or it will have a default size of 10 units.
```java ```java
ArrayList<String> names = new ArrayList<>(); ArrayList<String> names = new ArrayList<>();
ArrayList<Integer> ages = new ArrayList<>(5); ArrayList<Integer> ages = new ArrayList<>(5);
@ -22,6 +28,37 @@ Since ArrayList implements *List*, an ArrayList can be created using the followi
An ArrayList is dynamic, meaning it will grow in size if required and similarly shrink in size if elements are deleted from it. This is what makes it better to use than normal arrays. An ArrayList is dynamic, meaning it will grow in size if required and similarly shrink in size if elements are deleted from it. This is what makes it better to use than normal arrays.
we can delete existing element from the list
```java
variable_name.remove(index_number);
```
to access existing element from the list
```java
variable_name.get(index_number);
```
we can modify the existing element too
```java
variable_name.set(index_number,element);
```
we can reverse the order of elements in Array-list.
import java.util.Collections // package
```java
Collections.reverse(variable_name);
```
Sort the collection // in ascending order
```java
Collections.sort(variable_name);
```
for sorting in decending order
```java
Collections.reverseOrder());
```
An ArrayList allows us to randomly access elements. ArrayList is similar to *Vector* in a lot of ways. But it is faster than Vectors. The main thing to note is that - Vectors are faster than arrays but ArrayLists are not. An ArrayList allows us to randomly access elements. ArrayList is similar to *Vector* in a lot of ways. But it is faster than Vectors. The main thing to note is that - Vectors are faster than arrays but ArrayLists are not.
So when it comes down to choosing between the two - if speed is critical then Vectors should be considered, otherwise ArrayLists are better when it comes to storing large number of elements and accessing them efficiently. So when it comes down to choosing between the two - if speed is critical then Vectors should be considered, otherwise ArrayLists are better when it comes to storing large number of elements and accessing them efficiently.