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

  • What is Adjective in Hindi (विशेषण क्या है?)
    What is Adjective in Hindi (विशेषण क्या है?) Grammar
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
    Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी Biography
  • Holi kyon manate hain in hindi? | Festival of colors in hindi
    Holi kyon manate hain in hindi? | Festival of colors in hindi Festival
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans of freedom fighters in Hindi | स्वतंत्रता सेनानियों के प्रसिद्ध नारे हिंदी में General Knowledge
  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • Farmers Day
    National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती General Knowledge
  • Human rights day
    Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे General Knowledge

Java ArrayList

Posted on August 16, 2021October 20, 2021 By GeekCer Education 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 LinkedList Java LinkedList Java
Java 8 interview questions and answers, Java Stream, Optional Java 8 interview questions and answers, Java Stream, Optional Important
Difference between final, finally and finalize Difference between final, finally and finalize Differences
Java Learning Tutorial Java Tutorial for Advanced Learning, Buzzwords, Parts of Java Java
Multithreading in java Multithreading in java, Thread, Runnable | Life Cycle of Thread Java
Fork/Join Framework in Java | RecursiveTask, RecursiveAction Fork/Join Framework in Java | RecursiveTask, RecursiveAction 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
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • 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