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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. Things to Know About ArrayLists

Things to Know About ArrayLists

Want to make your Arrays more dynamic? Get a glimpse of Java's ArrayLists, including a practical demonstration and even a video showing it in action.

Damian Wolf user avatar by
Damian Wolf
·
Dec. 05, 16 · Tutorial
Like (4)
Save
Tweet
Share
16.88K Views

Join the DZone community and get the full member experience.

Join For Free

The ArrayList is one of the more interesting things a Java programmer can learn. If you have experience coding in Java, you should know what how to use Array or List. However, if you are new to Java, things might not be as easy.

So, what is Java ArrayList?

ArrayList is a dynamic Array that enables a programmer to store any number of items. Sound confusing? If yes, then we first need to understand static and dynamic concepts in computer science.

Image title

Static and Dynamic Data Structure

Data structures like Array are either static or dynamic. For example, an array can be used to store a group of elements. Generally, arrays are static in nature and hence will not allow you to add a new element. But arrays can also be set to dynamic, meaning you can alter its length and add new elements on the fly.

In Java, you can declare an array by using the following code:

Int[] Arr; //declaring array

Arr = new int[5]; //initializing an array with the size of 5.


Arr can now store 5 elements. You cannot add elements to the Arr array, making it static in nature.

So, how do you tackle the problem of creating a dynamic array? Java ArrayList does the job!

ArrayList

ArrayList expands the List interface, helping you take full advantage of it. Using the Array data structure can be good for some situations, but ArrayList provides better control over the data used in the program and lets you easily expand on it.

ArrayList improves on how an array works. As a programmer, the flexibility of adding new elements can be used extensively in different scenarios. ArrayList can also be useful in managing program memory consumption, as unnecessary elements are discarded when they are not needed. This improved memory consumption is great for real-time applications.

However, it should be noted that ArrayList should never be used for information that you don’t want to change. For example, if you are storing the number of months, i.e., 1 to 12, it is wise to use an array that provides static behavior.

Importing the Right Package

To make proper use of the ArrayList, you first need to import the package from the java.util library.

import java.util.ArrayList;


Once imported, all you need to do is create a new instance by using the following line of code:

ArrayList myList = new ArrayList();


This will create a new ArrayList of the name myList.

Operations

After we have created the ArrayList, we can use different methods on it. Let’s go through some of them.

add(object A)

myList.add(“new-item”);

myList.add(“placement”);

myList.add(“five”);

myList.add(5);


As you can see, we have used the add() method to add new elements to the ArrayList myList. However, we need to understand that all of them are objects and not pure data types. The first three elements are string objects and the fourth one is an integer object. If you want to add an element at a certain place, you need to use add method with passing two arguments.

add(int index, Object A) -- the first argument is the index and the 2nd argument is the object itself.

get(int index)


With the get() method, you can easily retrieve information with the help of an index. All you need pass is the index value and the element value will be retrieved.

myList.get(2)

⇒ five. //ArrayList index starts from 0.

remove(Object A)


With the help of remove(), you can remove elements. The remove method takes index as an argument and will help you manage program resources well.

myList.remove(0); //this will remove “new-item” string object.


There are many other methods that you can use on ArrayList. Some of them are listed below.

  • set(int index, Object A)

  • int indexOf(Object A)

You can find the whole list here.

Iterating Over the ArrayList

Is there a way to iterate over ArrayList? If yes, how to do so? You can use Iterator() object. All you need to do is import the Iterator class from the java.util library by using the following line of code.

import java.util.Iterator;


As mentioned earlier, the Iterator class has some methods that you can utilize to iterate over ArrayList.

hasNext() is the perfect method to work with ArrayList. All you need to run a loop and check if it returns true or false.

First, you need to attach the ArrayList to the iterator object.

Iterator ity = myList.iterator();


Now, we can use the “ity” iterator object.

while(ity.hasNext()) {
System.out.println(ity.next());
}


The next() method enables to go through all the elements in the list.

A quick example of how it looks like.

import java.util.*;

public class LearnArrayList {
   public static void main(String args[]) {
    ArrayList<String> myList= new ArrayList<String>();

/*Adding new elements to the myList*/
myList.add("Ruby");
myList.add("Java");
myList.add("JavaScript");

/* Quick way of displaying all the arraylist items */
System.out.println("Currently the array list has following elements:"+myList);

/*Removing elements*/
myList.remove("Ruby");


/*Adding elements using index*/
myList.add(0, "C++");
myList.add(1, "C");

System.out.println("The whole List is as follows:"+myList);

/*Removing element using index*/
myList.remove(1);

System.out.println("Current array list is:"+myList);
   }
}


Conclusion

We hope that you now have a better idea on how to make proper use of Java ArrayList. If you are still feeling confused, then you can watch typedeph, a software engineer from the US, work on an ArrayList datastructure.

Want to add something interesting to the story? Feel free to comment below!

Data structure Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Asynchronous Messaging Service
  • Building a RESTful API With AWS Lambda and Express
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • Public Key and Private Key Pairs: Know the Technical Difference

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: