Member-only story

Java Programming Language: Iterator Object

Jesse L
3 min readApr 13, 2022

--

Hi everyone, welcome back. In this tutorial, we will be going over the Iterator object in Java. The Iterator is an object that can be used to traverse through a collection of data such as an ArrayList. This leaves the Iterator as a good option when wanting to loop through a collection. With this introduction out of the way, let’s get into it.

Creating ArrayList

Let’s start by creating an ArrayList that we will loop through later. Before we can create the ArrayList, we need to make sure we have our ArrayList import, which is included within the java.util package:

import java.util.ArrayList;

Now, we can successfully create our ArrayList:

ArrayList<Integer> numbers = new ArrayList<Integer>();

So now, we have created an ArrayList variable of the ‘Integer’ type named numbers. When we create our ArrayList, we need to specify the data type. As we can see in the example above, we have used the ‘Integer’ data type. We can use other data types as well, such as the String data type.

Adding Elements Into The ArrayList

Now, let’s try to add elements into our ArrayList. We can do this by using the built in add() function that is provided. Let’s see an example:

--

--

Jesse L
Jesse L

Written by Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.

No responses yet