Program for List and ArrayList in Java.

Frequently asked Programs in JAVA.

Basic difference between List and ArrayList is that List is an Interface and ArrayList is a Collection class. ArrayList implements List Interface.

Declaration and Initialization of List and ArrayList

import java.util.ArrayList;
import java.util.List;
public class Sample3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
                {		
		List<String> tempList=new ArrayList<String>();
                tempList.add("Automation");
		tempList.add("Manaual Testing");
		tempList.add("Performance Testing");
		}
}

Traversing values in the List

import java.util.ArrayList;
import java.util.List;
public class Sample3 
{
	public static void main(String[] args) 
        {
		List<String> tempList=new ArrayList<String>();
                tempList.add("Automation");
		tempList.add("Manaual Testing");
		tempList.add("Performance Testing");
                
                // Access specific value from the List using get(index) method
		// where index starts from 0
               // Below statement will return third value (Performance Testing) from the list
		System.out.println(tempList.get(2));
				
               // Using foreach loop to traverse all the values in a List
		for (String string : tempList) {
			System.out.println(string);	
		}
        }
}

Using List with Integer values

We can use all the methods as it is for integer values as well.

import java.util.ArrayList;
import java.util.List;
public class Sample3 
{
	public static void main(String[] args) 
        {
                // Declare and Initialize for Integer values.
		List<Integer> intList = new ArrayList<Integer>();
		intList.add(50);
		intList.add(80);
		intList.add(100);

		// Traversing all the integer values using foreach loop
		for (Integer integer : intList) {
			System.out.println(integer.intValue());
		}
        }
}

Other Useful methods in the ArrayList

Methods DescriptionExample
List<DataType> var=new ArrayList<DataType>();Declaration of List having String and Integer datatypes. List<String> tempList = new ArrayList<String>();
List<Integer> intList = new ArrayList<Integer>();
add(String)
add(Integer)
To add values in the list.tempList.add("Data1");
add(index,value)To add values at a specific position in the list.tempList.add(3, "Data");
isEmpty()Return True if the list is empty. Else return False.tempList.isEmpty();
size()Returns the total number of values present in the listtempList.size();
get(index)Returns the value present at the given index.tempList.get(2);
foreach loopTo traverse all the values in the list.for (Integer value : intList) { System.out.println(value.intValue());}
clear()Used to remove all values from the list.intList.clear();
remove(index)Used to remove a value at the specified index.intList.remove(2);
set(index, newValue)It replaces existing value with newValue at given index.intList.set(1, 110);
subList(fromIndex, toIndex)Returns a sublist from the existing big list. It contains values fromIndex (including this index) till toIndex (excluding this index).List<Integer> subList= intList.subList(0, 2);

sublist will contain only 2 elements.
indexOf(value)Searches the given value in a list and returns its index values.tempList.indexOf("Data"));
intList.indexOf(80);
lastIndexOf(value) Searches the given value in a list and returns its last index values. tempList.lastIndexOf("Data"));
intList.lastIndexOf(80);

Java Programs:

TestNG:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


374total visits,7visits today

Leave a Reply

Your email address will not be published. Required fields are marked *