From c31aa711d67cc170ff772ab9ba635d18142bd821 Mon Sep 17 00:00:00 2001 From: vikash vaibhav Date: Tue, 16 Oct 2018 02:45:38 +0530 Subject: [PATCH] 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 --- .../guide/english/java/arraylist/index.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/client/src/pages/guide/english/java/arraylist/index.md b/client/src/pages/guide/english/java/arraylist/index.md index 7eb2641603..a45753ee19 100644 --- a/client/src/pages/guide/english/java/arraylist/index.md +++ b/client/src/pages/guide/english/java/arraylist/index.md @@ -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. + 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. + ```java ArrayList names = new ArrayList<>(); ArrayList 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. + 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. 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.