ArrayList (Dynamic Array) in JAVA
Submitted by moazkhan on Tuesday, February 17, 2015 - 00:16.
ArrayList(dynamic array) in JAVA
In this tutorial you will learn 1) What is an ArrayList? 2) Why we use ArrayList? 3) How we use ArrayList? What is an ArrayList? ArrayList is a data structure. The ArrayList is java class that is extended from AbstractList class. It is actually an array but the property that makes it different from the ordinary array is that its size can be changed dynamically. The ordinary array’s size in unchangeable once defined. But in case of array list the size of array increases as we elements inside the array and decreases when an element from the array is removed. Why we use ArrayList? The arrayList decreases the burden of the programmer by changing its size according to the requirements of the user. It has its built in functions associated with it which can easily let programmers to add or remove the elements. The elements can be retrieved at a faster rate and also provides faster mechanisms to insert data in ArrayList. How we use ArrayList in JAVA programs As the ArrayList is a predefined data structure so first we have to import its library that is ‘import java.util.ArrayList;’ , in case you are using the eclipse compiler then it will give you an option to automatically do it for you. Then the next thing we have to do is to declare the ArrayList, its syntax is given below as- //by default the size of arraylist is 10
- package test;
- import java.util.*;
- public class myclass {
- //ArrayList
- {
- arList.add("JAVA");
- arList.add("is");
- arList.add("exciting");
- arList.add("and ");
- arList.add("I");
- arList.add("love");
- arList.add("it");
- //String f;
- //f=(String) arList.get(2);
- //System.out.print(f);
- arList.remove(2);
- arList.remove("it");
- arList.clear();
- }
- }
Add new comment
- 90 views