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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • DataWeave Interview Question: Concatenate Elements of an Array
  • ConcurrentHashMap: Call Only One Method Per Key
  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Merge Multiple PDFs in MuleSoft

Trending

  • Why Database Migrations Take Months and How to Speed Them Up
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • Secrets Sprawl and AI: Why Your Non-Human Identities Need Attention Before You Deploy That LLM
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  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.

By 
Mahesh Sharma user avatar
Mahesh Sharma
·
Updated Aug. 09, 19 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
273.4K 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.

Related

  • DataWeave Interview Question: Concatenate Elements of an Array
  • ConcurrentHashMap: Call Only One Method Per Key
  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Merge Multiple PDFs in MuleSoft

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!