Skip to main content

ArrayList (Dynamic Array) in JAVA

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

  1. ArrayList myarList = new ArrayList<String>();
  2. //by default the size of arraylist is 10

ArrayList is a key word, next to it is the name and after equation sign we have the new key word which is telling us that a new object of the ArrayList class is being made and it has a data type of string. It has 18 functions associated with it but here I will be using and discussing the most important ones only.
Here in the code the arrayList is first declared and then using “.add()” method of the array list the strings are added. It must be noted that as the ArrayList has the type string so only strings are added. After putting in the elements we have retrieved the element which is in the second position using “.get()” function. It takes in the location and retrieves its value. The next portion is commented out which is also a way to get the element from the ArrayList and store it into the variable which is also of the type string and then the value of the string is displayed. ‘System.out.println ’ is used to get a neat output. We can also display the contents of whole ArrayList by just using its name and putting it into the argument of display function that is “System.out.print()”. Next to it I have demonstrated that there are two ways to delete the entry from the ArrayList. We can just give its index or the value itself which we want to delete. The length of the ArrayList is shown by the “size()” function and in the end I have cleared all the entries of the ArrayList by using “clear()” function.
  1. package test;
  3. import java.util.*;
  5. public class myclass {
  6. //ArrayList
  7. public static void main(String args[])
  8. {
  9. ArrayList arList = new ArrayList<String>();
  10. arList.add("JAVA");
  11. arList.add("is");
  12. arList.add("exciting");
  13. arList.add("and ");
  14. arList.add("I");
  15. arList.add("love");
  16. arList.add("it");
  17. System.out.print("The Second Element in the arrayList is: ");
  18. System.out.print(arList.get(2));
  19. System.out.println();
  20. //String f;
  21. //f=(String) arList.get(2);
  22. //System.out.print(f);
  23. System.out.print("The contents of whole arrayList is");
  24. System.out.print(arList);
  25. System.out.println();
  27. arList.remove(2);
  28. arList.remove("it");
  29. System.out.print("The contents of array after some deletions is :");
  30. System.out.print(arList);
  31. System.out.println();
  32. System.out.print("Now the Size of the array is: ");
  33. System.out.print(arList.size());
  34. arList.clear();
  36. }
  37. }

Screen Shot
Screen Shot


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.