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(10)
Sign in (optional)
showing 1-10 of 10 comments,   sorted newest to the oldest

Adithya
2015-01-19 08:50:47 
how to retrieve values from a table using list??Please answer..........(I created table in one class and created List in another class.i want to link both actually....
(0) (0) Reply
Lomesh Patel
2014-04-21 12:17:47 
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..?


(0) (0) Reply
Silam
2014-05-22 10:05:10 
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);
}

}

(0) (0) Reply
Manoj
2013-07-12 20:47:51 
retrieveValuesFromListMethod1 is a custom method written here. You can see the method in the above code given
(0) (0) Reply
sanjai
2013-07-11 12:45:43 
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
(0) (0) Reply
Giri Kumar Panem
2014-06-19 09:19:13 
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
(0) (0) Reply
Manish Kumar
2013-08-19 22:14:43 
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.
(0) (0) Reply
Manoj
2013-07-12 20:46:59 
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.
(0) (0) Reply
sanjai
2013-07-11 12:43:14 
what is retrieveValuesFromListMethod1? how it come... is this a inbuild method?
(0) (0) Reply
rishabh
2013-02-10 06:02:59 
Can I use iterator class to retrieve data
(0) (0) Reply
Add a new comment ...
Use Discussion Board for posting new aptitude questions.


Name   Email (optional)

5 + 9 = (please answer the simple math question)

Post Your Comment