Skip to toolbar

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:


1017total visits,1visits today

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 4
  • Page views today : 4
  • Total visitors : 48,849
  • Total page view: 67,263

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.