Java program to search an element from an ArrayList

In this java program, we are going to learn how to search an element from an ArrayList? By IncludeHelp Last updated : December 31, 2023

Problem statement

Given an ArrayList, and we have to find/search an element from the list using Java program.

Searching an element from an ArrayList

The ArrayList.contains() method is used to check whether an element is exists in the ArrayList or not, this method return "false" if element is not found, and returns "true" if element exists in the ArrayList.

Syntax:

boolean ArrayList.contains(element);

Java program to search an element from an ArrayList

import java.util.ArrayList;

public class SearchAnElement {

  public static void main(String[] args) {
    //ArrayList object 
    ArrayList arrList = new ArrayList();

    //adding elements in the list
    arrList.add("100");
    arrList.add("200");
    arrList.add("300");
    arrList.add("400");
    arrList.add("500");

    //searching element "300"
    boolean isFound = arrList.contains("300");
    if (isFound == false)
      System.out.println("Element is not found in the list");
    else
      System.out.println("Element is found in the list");

  }
}

Output

The output of the above example is:

Element is found in the list

Java ArrayList Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.