Disadvantages of arrays:
- Check the disadvantages of arrays in below article.
- Advantages and disadvantages of arrays in java
Advantages / Benefits of arraylist in java:
- We have some disadvantages of arrays like arrays are fixed in length. and we need to mention size of the array while creation itself.
- So we have some advantages of arraylist when compared to arrays in java.
- Here the major advantages of arraylist over arrays.
1.ArrayList is variable length
- One of the major benefit of arraylist is it is dynamic in size. we can increase as well as decrease size of the arraylist dynamically.
- Resizable.
- package collections;
- import java.util.ArrayList;
- public class ArrayListExample {
- /**
- * Advantages of arrayList over arrays in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- list.add(3);
- System.out.println(list.size());
- list.add(4);
- list.add(5);
- System.out.println(list.size());
- list.remove(1);
- System.out.println(list.size());
- }
- }
Output:
- 3
- 5
- 4
- In the above program by using add and remove methods of ArrayList we can change the size of the ArrayList
2.Default initial capacity is 10.
- One of the major benefit of arraylist is by default it will assign default size as 10.
- Whenever we create object of ArrayList constructor of ArrayList assign default capacity as 10.
3.Insert and remove elements also at particular position of ArrayList
- Another advantage of ArrayList is it can add and remove elements at particular position.
Program #2: Java example program to explain the advantages of ArrayList:Add and remove elements at particular position.
- package collections;
- import java.util.ArrayList;
- public class ArrayListExample {
- /**
- * Advantages of arrayList over arrays in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- list.add(3);
- System.out.println(list);
- list.add(0,4);
- System.out.println(list);
- list.add(2,5);
- System.out.println(list);
- list.remove(3);
- System.out.println(list);
- System.out.println(list.size());
- }
- }
Output:
- [1, 2, 3]
- [4, 1, 2, 3]
- [4, 1, 5, 2, 3]
- [4, 1, 5, 3]
- 4
4.Add any type of data into ArrayList.
- We can add different type of objects in to the ArrayList.
- In this scenario avoid mentioning generics while declaring ArrayList.
Program #3: Java example program to explain the advantages of ArrayList: Add any type of Object.
5.Traverse in both directions.
- We can traverse both direction using List Iterator.
Program #4: Java example program to explain the advantages of ArrayList: Traverse in both directions.
- package collections;
- import java.util.ArrayList;
- import java.util.ListIterator;
- public class ArrayListExample {
- /**
- * Advantages of arrayList over arrays in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add("ArrayList Advantages");
- list.add("ArrayList benefits");
- list.add("ArrayList in java");
- ListIterator iterator = list.listIterator();
- System.out.println("**ArrayList Elements in forward direction**");
- while (iterator.hasNext())
- {
- System.out.println(iterator.next());
- }
- System.out.println("**ArrayList Elements in backward direction**");
- while (iterator.hasPrevious())
- {
- System.out.println(iterator.previous());
- }
- }
- }
- **ArrayList Elements in forward direction**
- ArrayList Advantages
- ArrayList benefits
- ArrayList in java
- **ArrayList Elements in backward direction**
- ArrayList in java
- ArrayList benefits
- ArrayList Advantages
6.ArrayList allows Multiple null values
- We can add multiple null elements to ArrayList
Program #5: Java example program to explain the benefits of ArrayList: Add null elements
- package collections;
- import java.util.ArrayList;
- public class ArrayListExample {
- /**
- * Advantages of arrayList over arrays in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- ArrayList<String> arraylist = new ArrayList<String>();
- arraylist.add(null);
- arraylist.add(null);
- arraylist.add(null);
- System.out.println(arraylist);
- }
- }
Output:
- [null, null, null]
7.ArrayList allows to add duplicate elements
- We can add duplicate elements into arrayList
- package collections;
- import java.util.ArrayList;
- public class ArrayListExample {
- /**
- * Advantages of arrayList over arrays in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- ArrayList<String> arraylist = new ArrayList<String>();
- arraylist.add("ArrayList");
- arraylist.add("ArrayList");
- arraylist.add("ArrayList");
- System.out.println(arraylist);
- }
- }
Output:
- [ArrayList, ArrayList, ArrayList]
8.ArrayList has many methods to manipulate stored objects.
- ArrayList has many methods to manipulate stored objects.
- addAll(), isEmpty(). lastIndexOf() etc.
No comments