Update index.md (#29031)
This commit is contained in:
committed by
Manish Giri
parent
60c796e70d
commit
2353a3c02a
@ -9,20 +9,18 @@ title: ArrayList
|
||||
```java
|
||||
import java.util.ArrayList; // is more efficient than importing all of java.util
|
||||
```
|
||||
|
||||
Always import the most specific package in order to save memory size and performance 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 should be initialized to have a specific size or it will have the default size of 10 units.
|
||||
|
||||
|
||||
Always import the most specific package in order to save memory size and performance 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 should be initialized to have a specific size or it will have the default size of 10 units.
|
||||
|
||||
|
||||
```java
|
||||
ArrayList<String> names = new ArrayList<>();
|
||||
ArrayList<Integer> ages = new ArrayList<>(5);
|
||||
```
|
||||
|
||||
In the above snippet, the angle breackets `<>` take a generic data type as argument specifying data type of the elements in the ArrayList. The first ArrayList `names` is specified as containing *String* elements. Thus, it will only be allowed to contain String elements. Its size is not specified so it will have a default size of 10. The second ArrayList `ages` has specified that it will only hold integers. But ArrayList cannot hold primitives, it only holds objects. Thus to make it store integers, floats, etc., we can use wrapper classes. `names` will have a specified size of 5.
|
||||
In the above snippet, the angle brackets `<>` take a generic data type as argument specifying data type of the elements in the ArrayList. The first ArrayList `names` is specified as containing *String* elements. Thus, it will only be allowed to contain String elements. Its size is not specified so it will have a default size of 10. The second ArrayList `ages` has specified that it will only hold integers. But ArrayList cannot hold primitives, it only holds objects. Thus to make it store integers, floats, etc., we can use wrapper classes. `names` will have a specified size of 5.
|
||||
|
||||
Since ArrayList implements *List*, an ArrayList can be created using the following syntax:
|
||||
```java
|
||||
|
Reference in New Issue
Block a user