DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Create an ArrayList in Java

How to Create an ArrayList in Java

This widely-used function adds functionality and flexibility to your applications.

Mahesh Sharma user avatar by
Mahesh Sharma
·
Aug. 09, 19 · Tutorial
Like (14)
Save
Tweet
Share
262.90K Views

Join the DZone community and get the full member experience.

Join For Free

To store dynamically-sized elements in Java, we used ArrayList. Whenever new elements are added to it, its increase their size automatically. ArrayList implements Java’s List Interface and part of Java’s Collection.

Because of their functionality and flexibility, it is widely used.

Key Points of an ArrayList

  1. An ArrayList is a re-sizable array, also known as a dynamic array. It raises its size according to new elements and decreases the size when the elements are removed.
  2. An array is used to store an element in ArrayList internally. It allows you to retrieve the elements by their index.
  3. Java ArrayList class permits duplicate and null values.
  4. Java ArrayList class is a well-ordered collection. It keeps the insertion order of the elements.
  5. In ArrayList, you cannot create an ArrayList of primitive types like int, char, boolean, etc. You must use boxed types like Integer, Character, Boolean etc.

Hierarchy of ArrayList


Hierarchy of java Arrary List

ArrayList implements the List Interface extends Collection extends Iterable.

How to Create an ArrayList

In Java, we can create ArrayList by creating this simple statement:

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

In above syntax, list is of “String” type, so the elements are that going to be added to this list will be string type. The type decide which type of elements list will have.

ArrayList<String> arlist = new ArrayList<Integer>( );

Above syntax, is accepts int elements.

How to Add Elements?

To add an element in ArrayList, we can use add( ) method. This method has variations, which is use depends on requirements.

Syntax

arlist.add(“JavaTpoint”);

Add elements at the particular location, we can write method like this:

arlist.add(2, “JavaTpoint”);

Example 1

class ArrayList1{ 

   public static void main(String args[]){ 

      ArrayList<String> ArrayList<String>(); 

      arlist.add("JAVA");

      arlist.add("Csharp");

      arlist.add("Python");

      arlist.add("Php");

      arlist.add("Android");

      arlist.add("HTML");

      //Adding "C++" at the sixth position

      arlist.add(5, "C++");

      //displaying elements

      System.out.println(arlist);

   } 

}

Output:

[JAVA, Csharp, Python, Php, Android, C++, HTML]

How to Remove Elements

To add an element in ArrayList, we can use the remove( ) method. This method also has variations.

class ArrayList1{ 

   public static void main(String args[]){ 

      ArrayList<String> ArrayList<String>(); 

      arlist.add("JAVA");

      arlist.add("Csharp");

      arlist.add("Python");

      arlist.add("Php");

      arlist.add("Android");

      arlist.add("HTML");

      //remove "C++" from the sixth position

      arlist.remove("C++");

      //displaying elements

      System.out.println(arlist);

   } 

}



class ArrayList1{ 

   public static void main(String args[]){ 

      ArrayList<String> ArrayList<String>(); 

      arlist.add("JAVA");

      arlist.add("Csharp");

      arlist.add("Python");

      arlist.add("Php");

      arlist.add("Android");

      arlist.add("HTML");

      //remove "C++" from the sixth position

      arlist.remove("C++");

      //displaying elements

      System.out.println(arlist);

   } 

}

Output: [JAVA, Csharp, Python, Php, Android, HTML]

Methods of Java ArrayList

There are many methods in Java ArrayList, but we explain here some main methods:

Int Siz()

returns the elements present in the list.

Void trimToSize( )

Used to trim the capacity from list’s current size to ArrayList instance.

E set(int index, element)

replace the elements with the specified position.

boolean remove(Object o)

remove the first instance of the detailed element.

set(int index, Object o)

It is used to update an element. Replace the element with object o.

Object get(int index)

returns the object of the list

boolean isEmpty( )

returns true if list is empty.

Constructors of Java ArrayList

ArrayList( )

built an empty array list.

ArrayList(Collection<? extends E>c )

built an array list that is initialized with the elements of the collection c

ArrayList(int capacity)

built array list that has the specified initial capacity.

Further Reading

Different Approaches to Sorting Elements of an Arraylist in Java

ArrayList vs. LinkedList vs. Vector



If you enjoyed this article and want to learn more about Java Collections, check out this collection of tutorials and articles on all things Java Collections.

Data structure Java (programming language) Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Remote Debugging Dangers and Pitfalls
  • Taming Cloud Costs With Infracost

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: