Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Java ArrayList

  • Python List
    Python List Python
  • Java Operators
    Java Operators Java
  • JSON The Data Interchange Standard Format
    JSON The Data Interchange Standard Format Important
  • JSP Directives
    JSP Directives JSP
  • Difference between Bugs, Errors and Issues in Software Testing
    Difference between Bugs, Errors and Issues in Software Testing Important
  • Mahatma Gandhi Essay in Hindi | Gandhiji Biography
    Mahatma Gandhi Essay in Hindi | Gandhiji Biography Biography
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • Christmas Day Celebration
    Christmas Day Celebration (क्रिसमस दिवस क्यों मनाते हैं?) Festival

Java ArrayList

Posted on August 16, 2021October 20, 2021 By admin No Comments on Java ArrayList
Java ArrayList

A Java ArrayList is a growable array that grows dynamically according to the number of elements we add and insert elements. If we talk about memory, when we add or insert any element in ArrayList then it dynamically allocate and reallocate memory to hold all the elements.

The Arraylist inherits the methods from the List interface. You can access the elements of an ArrayList by using the get() and set() methods. ArrayList is not synchronized, so if multiple threads work simultaneously the result may be wrong in some cases.

In this article we will discuss about key points, constructors, methods available in ArrayList class, generic ArrayList and when you should and when you should not go for ArrayList. We will also discuss the difference between ArrayList and LinkedList.

Table of Contents

  • Basic points to remember about Java ArrayList
  • Constructors of ArrayList
          • Note:
  • Methods of ArrayList class
    • Example program using Java ArrayList
    • Generic ArrayList in Java
    • When should you use ArrayList?
    • When should you not use ArrayList?
  • Difference between ArrayList and LinkedList

Basic points to remember about Java ArrayList

  • It follows the insertion order and it can store duplicate elements, so you do not need to worry about the order of the elements.
  • We can insert null value in the ArrayList, and we can have any number of null value.
  • ArrayList implements RandomAccess interface so you can access random elements by using index.
  • You can add Heterogeneous objects to the ArrayList. In other words, you can add any types of object into it, it may be Double type, String type, Integer type and so on.

Constructors of ArrayList

The following constructors are available for ArrayList, including a constructor without arguments, a constructor that accepts an initial capacity, and a constructor that accepts a collection:


ArrayList list = new ArrayList();

ArrayList list = new ArrayList(int initialCapacity);

ArrayList list = new ArrayList(Collection c);

Note:

Default initial size of ArrayList is 10.
Once ArrayList reaches it’s maximum capacity then a new ArrayList object will be created with
new capacity = current capacity * 3/2 + 1

Methods of ArrayList class

Here is the list of methods available in ArrayList class, by using these methods we store, insert, delete, retrieve elements from ArrayList.

#MethodsDescription
1.boolean add(E e)This method adds elements to the end of the ArrayList, and returns true if successfully added.
2.void add(int position, E obj)This method inserts element at the specified position.
3.E remove(int position)This method removes an element from the specified position and then returns the removed element.
4.boolean remove(Object obj)You can use this method to removes the specified object and then returns true/false.
5.void clear()Once you call this method, the ArrayList will be empty.
6.boolean contains(Object obj)This method returns true if ArrayList contains the specified element.
7.E get(int position)It returns the element available at the specified position.
8.int size()It returns the number of element available in the ArrayList.

Example program using Java ArrayList

This is an example program of Java ArrayList in which we add four items and display the items of ArrayList on screen.

                  import java.util.ArrayList;

public class ArrayListProgram {
  public static void main(String args[]) {
    ArrayList arrayList = new ArrayList();
    arrayList.add("Item 1");
    arrayList.add("Item 2");
    arrayList.add("Item 3");
    arrayList.add("Item 4");

    System.out.println("ArrayList:  " + arrayList);
  }
}

Output:
ArrayList:   [Item 1, Item 2, Item 3, Item 4]

Generic ArrayList in Java

Generic ArrayList allows us to hold the elements of only one type that we specify. You can write the Generic ArrayList by using following syntax:


class ArrayList<E>

Here E represents the type of elements that ArrayList will contains. The following is an example of an ArrayList that can store objects of String type and Double type.


ArrayList<String> list = new ArrayList<String>();

ArrayList<Double> list = new ArrayList<Double>(30);

When should you use ArrayList?

If you need to get the elements from the list frequently then you should choose ArrayList. Because you can get element by providing index of the element which is faster.

When should you not use ArrayList?

If you insert or delete the elements frequently in the middle of list then you should not use ArrayList because during insertion and deletion it performs many shift operation.

Difference between ArrayList and LinkedList

There are following differences between ArrayList and LinkedList:

#ArrayListLinkedList
1ArrayList uses a dynamic array to store the items.LinkedList uses a doubly linked list to store the items.
2ArrayList is slow because array manipulation is slower.But LinkedList is faster because it is node based which does not require much shifting of items.
3ArrayList implements List interface.But LinkedList implements List as well as Queue. We can use LinkedList to implement Queue functionality.
4ArrayList is best when we frequently store and access items.LinkedList is best suitable when we frequently manipulate items.

References:
https://docs.oracle.com/javase/7/docs/api/java/util/…

In conclusion, hope this article will help you to understand ArrayList class and we have tried to cover all basic information about ArrayList which can help you in interview as well.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java

Post navigation

Previous Post: Collections in Java
Next Post: Java LinkedList

More Related Articles

Java If-else Statement Java If-else Statement Java
Jenkins java | Installing Jenkins on Windows Jenkins java | Installing Jenkins on Windows Important
Difference between GenericServlet and HttpServlet Difference between GenericServlet and HttpServlet Differences
Java Switch Case Java Switch Case Java
Lambda Expression in Java 8 | Functional Interface | Example Lambda Expression in Java 8 | Functional Interface | Example Java
Iterate Map in Java Iterate Map in Java Java

Related Posts

  • Java If-else Statement
    Java If-else Statement Java
  • Jenkins java | Installing Jenkins on Windows
    Jenkins java | Installing Jenkins on Windows Important
  • Difference between GenericServlet and HttpServlet
    Difference between GenericServlet and HttpServlet Differences
  • Java Switch Case
    Java Switch Case Java
  • Lambda Expression in Java 8 | Functional Interface | Example
    Lambda Expression in Java 8 | Functional Interface | Example Java
  • Iterate Map in Java
    Iterate Map in Java Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Python List
    Python List Python
  • Java Operators
    Java Operators Java
  • JSON The Data Interchange Standard Format
    JSON The Data Interchange Standard Format Important
  • JSP Directives
    JSP Directives JSP
  • Difference between Bugs, Errors and Issues in Software Testing
    Difference between Bugs, Errors and Issues in Software Testing Important
  • Mahatma Gandhi Essay in Hindi | Gandhiji Biography
    Mahatma Gandhi Essay in Hindi | Gandhiji Biography Biography
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • Christmas Day Celebration
    Christmas Day Celebration (क्रिसमस दिवस क्यों मनाते हैं?) Festival
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved