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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • DataWeave Interview Question: Concatenate Elements of an Array
  • Dynamic Arrays, Spill, and LET: What Changed in Excel and Why It Matters for Java Applications
  • Arrays in Java
  • Immutable Objects Using Record in Java

Trending

  • Share Delta Lake Tables Between Databricks and Snowflake
  • DuckDB for Python Developers
  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale
  • Designing Frontend Applications for Failure, Not Just Success

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Aug. 09, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
275.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Dynamic Arrays, Spill, and LET: What Changed in Excel and Why It Matters for Java Applications
  • Arrays in Java
  • Immutable Objects Using Record in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook