Insert and Retrieve Values from a List (ArrayList)

Overview

This Java program does the followings

1. Inserts values into an ArrayList

2. Retrieve the values of ArrayList using Iterator

3. Retrieve the values of ArrayList using get method

4. Retrieve the values of ArrayList using Enumeration

Java Program

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class ArrayListDemo
{

	public static void main(String args[])
	{
		List list = new ArrayList();

		//Adding values to the ArrayList
		list.add("test1");
		list.add("test2");
		list.add("test3");

		System.out.println("Retrieving values from ArrayList using Iterator");
		retrieveValuesFromListMethod1(list);
		System.out.println("**********************\n\n");

		System.out.println("Retrieving values from ArrayList using get method");
		retrieveValuesFromListMethod2(list);
		System.out.println("**********************\n\n");

		System.out.println("Retrieving values from ArrayList using Enumeration");
		retrieveValuesFromListMethod3(list);
		System.out.println("**********************\n\n");
	}


	/*This method retrieves values from ArrayList using Iterator
	 */
	public static void retrieveValuesFromListMethod1(List list)
	{
		Iterator itr = list.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}

	/*This method retrieves values from ArrayList using get method
	 */
	public static void retrieveValuesFromListMethod2(List list)
	{
		//Retrieving values from list
		int size = list.size();
		for(int i=0;i<size;i++)
		{
			System.out.println(list.get(i));
		}
	}


	/*This method retrieves values from ArrayList using Enumeration
	 */
	public static void retrieveValuesFromListMethod3(List list)
	{
		Enumeration e = Collections.enumeration(list);
		while(e.hasMoreElements())
		{
			System.out.println(e.nextElement());
		}

	}

}

Output

Output of the Program

Questions which are answered by This Sample Program

This program clarifies the following queries.

1. How to insert values to an ArrayList in Java / How to insert values to a List in Java

2. How to retrieve values from an ArrayList / How to retrieve values from a List

3. How to retrieve values from an ArrayList using Enumeration / How to retrieve values from a List using Enumeration

4. How to retrieve values from an ArrayList using Iterator / How to retrieve values from a List using Iterator

5. How to retrieve values from an ArrayList using get method / How to retrieve values from a List using get method


Comments(15)


jugga 24 Apr 2014 10:42 AM
great work
Like (0)| Dislike (0)| Reply| Flag

Lomesh Patel 21 Apr 2014 2:47 PM
Hello Sir,

How to Store Values from Array to List.

i have values in array but i will store that values in list.
so, how can i..?


Like (0)| Dislike (0)| Reply| Flag

Silam 22 May 2014 12:35 PM
package ran;

import java.util.ArrayList;
import java.util.List;

public class Arrayltol {
public static void main(String a[]){
ArrayList myar=new ArrayList();
myar.add("dsgfd");
myar.add("sds");
myar.add("sf");
myar.add(0,0);
System.out.println(myar);
List<String> list=new ArrayList<String>();
list.add("zsfs778549748");
list.addAll(myar);
System.out.println(list);
}

}

Like (0)| Dislike (0)| Reply| Flag

Surekha 21 Oct 2013 5:57 PM
Simplified structure language.
Like (0)| Dislike (0)| Reply| Flag

senattu 15 Jul 2013 6:20 PM
 hello
Like (0)| Dislike (0)| Reply| Flag

Manoj 12 Jul 2013 11:17 PM
retrieveValuesFromListMethod1 is a custom method written here. You can see the method in the above code given
Like (0)| Dislike (0)| Reply| Flag

sanjai 11 Jul 2013 3:15 PM
while(itr.hasNext())
        {
            System.out.println(itr.next());
        }

Hi. am new to JAVA.
I dont understand this line... what is the difference between hasNext() and next(). Can anyone answer for this
Like (0)| Dislike (0)| Reply| Flag

Giri Kumar Panem 19 Jun 2014 11:49 AM
hasNext()-> it returns true if iterator has more elements otherwise false.This hasNext() method is boolean type.
next()->it returns the element and moves the cursor pointer to the next element
Like (0)| Dislike (0)| Reply| Flag

Manish Kumar 20 Aug 2013 12:44 AM
iterator is a interface in java .Threw iterator itr=list.iterator()we can't store value inside iterator. iterator is pointing to the arraylist inserted  values. hasNext() method return type is boolean .iterator is started  pointing first value of list then if iterator is pointing any value then hasNext() method return true.if hasNext() method return true then while loop condition is true than flow comes inside the loop .   next() method print the  pointing value of iterator.
  then next time iterator point next value of arraylist and hasNext() method retun true.
Like (0)| Dislike (0)| Reply| Flag

Manoj 12 Jul 2013 11:16 PM
hasNext() returns true if the iteraton has more elements. False if the iterator has no more elements

next(0 returns the next element in the iteration.
Like (0)| Dislike (0)| Reply| Flag

next last
Add a new comment ...


Name   Email

Please answer the simple math question given below

9 + 0 =


close

Sign in