Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 18

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 17 | Lesson 19 >>

_______________________________________

Arrays lists and iterators in Java

Arrays lists and iterators in Java

Lesson 18: Arrays lists and iterators in Java

In the previous examples on arrays, we have been working with known sizes of arrays but what if we don’t know how many positions will be there in the array?

Here, we introduce Array lists. Array lists are dynamic structures for store of values, that means values can be added or be removed from the list.

We do not even need to declare the size of the list but values can be accessed by their index positions. For us to use Array list in the program, we have to import Java Array list class into the program using the following statement:

import java.util.ArrayList;

Then, to use the Array list we need to create a new ArrayList object:

ArrayList myList = new ArrayList( );

That is all we need to have our Array list working, notice that this time we don’t need square brackets like we had in the other previous examples of arrays.

To populate our list with values, we use one of the methods of Array list; add(). We are going to add the seven days of the week into our list using this method and then manipulate our list.

myList.add( "Monday" );

myList.add( "Tuesday" );

myList.add( "Wednesday" );

myList.add( "Thursday" );

myList.add( "Friday" );

myList.add( "Saturday" );

myList.add( "Sunday" );

Two methods of Array list; get() and remove() can be used to manipulate values in the Array list.

To get the value of the Array list at index position 3, we use the following statement:

myList.get(3);

To remove the value of the Array list at index position 2, we use the following statement:

myList.remove(2);

The method remove() is flexible enough that you can even remove an item from the Array list using its actual name:

myList.remove(“Wednesday”);

Create another class, call it arrayLists and write the following code. I have decided to use JOptionPane just to refresh your mind of what we learnt in earlier lessons.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-18
Arrays lists and iterators in Java

Arrays lists and iterators in Java

To output all the items from an Array list, we have to set an iterator to go through the list items. The following statement imports an iterator class:

import java.util.Iterator;

Then, we create and object of the iterator class and associate it with our list using the following statement:

Iterator icount = myList.iterator( );

Having done that, we are going to use a WHILE loop and two methods of iterator; hasNext() and next() to loop through the list and output the list items. The method hasNext() returns a true value if the list has any item otherwise it returns a false value. The method next() is used to loop through the list items one by one.

while ( icount.hasNext( ) ) {
icount.next( );
}

System.out.println( "Our list has " +myList );

Create another Java class and try the following example:

Java program using Arraylist and Iterator

package myfirstprogram;
import java.util.Iterator;
import java.util.ArrayList;
public class arrayLists {
    public static void main(String[] args) {
       ArrayList myList = new ArrayList( );
       
        myList.add( "Monday" );
        myList.add( "Tuesday" );
        myList.add( "Wednesday" );
        myList.add( "Thursday" );
        myList.add( "Friday" );
        myList.add( "Saturday" );
        myList.add( "Sunday" );
        
        Iterator icount = myList.iterator( );
        while ( icount.hasNext( ) ) {
            icount.next( );
        }
            System.out.println("Our list has " + myList); 
    }  
}
Arrays lists and iterators in Java

Arrays lists and iterators in Java

I hope you have enjoyed working with Array lists and you have seen how useful they are when the number of array positions is not known. In the next lesson, we shall look at various Java methods used with strings, see you then.

<< Lesson 17 | Lesson 19 >>

Related hubs...

Want to make money online?

Comments

Danson Wachira (author) from Nairobi, Kenya on September 06, 2012:

Hello Lord De Cross, how thoughtful of you, i was just thinking of starting a HTML/CSS/JAVASCRIPT/PHP tutorial. I need to plan about that. Thanks for these suggestions. I do appreciate you stopping here and commenting. Lala unono! (sleep tight)

Joseph De Cross from New York on September 06, 2012:

Dwachira my friend, we suggest you to help us with some html coding. Our Hubber friends would appreciate it so much. Cheer up and Siku njema!

mwahhh on September 06, 2012:

i like your good job. Wachira

Danson Wachira (author) from Nairobi, Kenya on September 02, 2012:

Thanks girishpuri for the visit and comment, am glad you found this article to be useful. Enjoy your day.

Danson Wachira (author) from Nairobi, Kenya on September 02, 2012:

Thanks teaches12345 for your continued support here, i do appreciate the visit and comment.

Girish puri from NCR , INDIA on August 31, 2012:

Very much useful an educative hub, voted useful.

Dianna Mendez on August 31, 2012:

Just stopped in to read and support the hub topic. I understand the flow here and know that it will be useful to many.

Joseph De Cross from New York on August 30, 2012:

Siku njema to you!

Danson Wachira (author) from Nairobi, Kenya on August 30, 2012:

Hi Lord De Cross, i have been away for some days and nothing i missed like Hubpages and my fellow friends and hubbers, am afraid it might be true that am addicted to Hubpages. It is good to hear from you my friend and thanks for the visit and comment. Siku njema!

Joseph De Cross from New York on August 30, 2012:

I applaud your [ArrayList LordList (effort); Dwachira] loop; End.

Don't worry,

The List answer is = shake hands!

~~ Hello World!

Have a great day my friend!:)