Java Loop Arraylist Example

HQ » Java Tutorial » Example Source Code » Java Array Examples » Loop through an ArrayList
Java Loop Arraylist Example 2018-02-16T07:27:23+00:00

On this section we will be showing some java examples on how to iterate or loop through an arraylist. This is one of the most important knowledge in dealing with list and arrays on how to loop for each elements. Why would be needing to loop through the elements of an arraylist? Well, to make it simple it is for the intention of accessing each member of the the list. Either we would be using the elements to do some complicated logical program or simply just to print it.

Background of ArrayList

From official java documentation the following best describes an ArrayList:

  • Resizable-array implementation of the List interface.
  • Implements all optional list operations
  • permits all elements including null
  • In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list

Ways to loop through an ArrayList

There are several ways of iterating through the elements, below are some of it

  • ArrayList iteration through for loop
  • Using while loop
  • Using do while loop in interation
  • And the advance for loop

Java Examples in looping through an ArrayList

The following are comprehensive examples in dealing with ArrayList

Loop through an ArrayList using for statement

Basically on this example we declared an ArrayList of fruits and then we just iterate through the elements using for loop. After which we just prints out those elements. Its worthwhile that you might need to take a look on how we have used the add method, size of ArrayList and how we have used Generics ArrayList<String>.

Sample Output

Running the above code we will be having the following result

Sample Output of itereate arraylist using for-loop

Sample Output of itereate arraylist using for-loop

 

Loop through an ArrayList using while statement

On this example, we basically just iterate through a List of Integer using while loop and then print each individual elements.

In dealing with while loop, it’s a little bit harder than the for loop because small mistake like for example the condition

if we have put the index <= maxIndex, the output would throw an ArrayIndexOutOfBounds exception. But it doesn’t mean that you could not use the index <=maxIndex, its just a matter of adjustment. see below for  a sample snippet that would work for this condition

To compensate for our modified condition, we have started the index to 1 instead of 0. However in order to get the first index we have used get(index -1) because remember that for ArrayList the index starts at 0.

Sample Output

Running the above code we will be having the following result

Arraylist while loop sample output

Arraylist while loop sample output

 

Loop through an ArrayList using do while statement

On this example, we basically just iterate through a List of String using do while loop and then print each individual elements. This is similar to while loop but on this case we will print first the element before iteration. Dealing with this loop is a little bit tricky which we will be showing in later discussion. First lets go through first with this example:

Running the above example we will be having the following output.

arraylist do while loop sample output

arraylist do while loop sample output

 

You would have noticed how the printing of elements comes first before the increment of the index. If you put the increment before printing, IndexOutOfBoundsException will be thrown. Why? because if we interchange the print and index iteration, the index would start at 1. Remember that the first element is 0. Thus if we start at 1, we would be accessing an element of a List that does not exist. Be careful in dealing with loops. It takes a lot of practice in order to get this right and avoid the horrible IndexOutOfBoundsException, which I believe you would be getting a lot in the beginning of your programming experience.

Using advance for loop

Now my favorite looping mechanism the advance for loop or sometimes called enhance for loop but techincally being called for each loop. This is easy to use and no need to worry on indexes, we just iterate through the elements. Now here is a concrete example on which looping strategy is much better.

Running the above example code we will be having the following output on our console:

arraylist advance for loop sample output

arraylist advance for loop sample output

 

Conclusion:

There are multiple ways in looping through an arraylist. We have provided 4 ways however as we have gone through the examples, we have shown also some possible modifications on our codes and that would give more complications in handling ArrayList loops. On the last part we have show the for-each loop which is easy to use. However if we are dealing with index i would suggest other methods because they are more versatile.

In short, there is no best method here and the choice would be at the hands of the programmer. As long as we handle properly the IndexOutOfBoundsException which will definitely makes every programmer crazy, we are on the right track.

Based on my personal preference i am using for loop and the enhance for loop depending on the requirements. In order to master these looping methods, I would suggest do more practice. If you gain years of experience, understanding these would be a piece of cake.

 [/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close