How to Search String in ArrayList in Java with Example code

/* Searching an element in ArrayList without using “contains(Object elem)”, “indexOf(Object elem)” methods can be done by traversing the array list until the search string matches with arraylist element. If does not match, search string does not exist in the array list. The Following code finds an element in ArrayList and returns the position. If the element does not exist, it returns the position 0*/

Example 1 :

package com.javaonline;

import java.util.*;

public class SearchArrayList1{

public static void main(String[] args) 

{

String searchString="";
ArrayList<String> arrayList = new ArrayList<String>();

arrayList.add("Delhi");  //Adding  elements to Arraylist
arrayList.add("Kolkata");
arrayList.add("Bangalore");
arrayList.add("Mumbai");
arrayList.add("Chennai");
arrayList.add("Hyderabad");
arrayList.add("Pune");

searchString="Mumbai";
search(searchString, arrayList);

searchString="Bhopal";
search(searchString, arrayList);

}

public static void search(String searchStr, ArrayList<String> aList)
{

boolean found = false;
Iterator<String>  iter = aList.iterator();
String curItem="";
int pos=0;

while ( iter .hasNext() == true )
{
	pos=pos+1;
	curItem =(String) iter .next();
	if (curItem.equals(searchStr)  ) {
	found = true;
	break;
		}

}

if ( found == false ) {
pos=0;
}

if (pos!=0)
 {
	System.out.println(searchStr + " City Found in position : " + pos);
 }
else
 {

	System.out.println(searchStr + " City not found");
 }

}

}

Running the Above program will result the following

search array list

Example 2 :

/* Latest JDK version does have the methods “contains(Object elem)” , “indexOf(Object elem)” , “lastIndexOf(Object elem) “, Using the methods, we can easily search an element in an ArrayList. The following code gives the idea of How to search an element in an ArrayList.

boolean contains(Object element) method is used to find out whether an element exists in Array List or not. It does not tell the position. It returns true if the ArrayList contains the specified object, otherwise, it returns false

int indexOf(Object element) method is used to get an index of specified element in ArrayList. This method returns the index (position) of the specified element in ArrayList. It returns -1, if not found.

int lastIndexOf(Object element) method returns index of the last occurrence of the specified element in ArrayList. It returns -1, if the eelement not found

Reference :  https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

*/

package com.javaonline;

import java.util.ArrayList;

public class SearchArrayList2{
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();


try
{
	arrayList.add("India");  //Adding  elements to Arraylist
	arrayList.add("US");
	arrayList.add("England");
	arrayList.add("Australia");
	arrayList.add("China");
	arrayList.add("France");
	arrayList.add("Singapore");
	arrayList.add("US");
}
catch (Exception e) 
{
}

boolean Found = arrayList.contains("China");
System.out.println("ArrayList contain China? : " + Found);

int index = arrayList.indexOf("China");
if(index == -1)
	System.out.println("ArrayList does not contain China");
else
	System.out.println("ArrayList contains China at index :" + index);

index = arrayList.indexOf("Malasia");
if(index == -1)
	System.out.println("ArrayList does not contain Malasia");
else
	System.out.println("ArrayList contains Malasia at index :" + index);

int lastIndex = arrayList.lastIndexOf("US");
if(lastIndex == -1)
	System.out.println("ArrayList does not contain US");
else
	System.out.println("Last occurrence of US in ArrayList is at index :"  + lastIndex);
} 

}


Running the above program will result the following output

search array list 1

You may also like

Leave a Reply