java

How to search a particular element in an array in Java

In this tutorial, we are going to see how to search a particular element in an array in Java. In the following example, we create an array of languages and look to see if the “Java” language is in the array. If so, we display that the language “Java” exists in the array.
 

Java Program to search a particular element in an array :
public class Main 
{
	public static void main(String[] args) 
	{
		String[] lang = {"PHP", "Java", "C++", "Python"};
		int exist = 0;
		for (int i=0; i < lang.length; i++)
		{
			if(lang[i].equals("Java"))
			{
				exist++;
			}
		}
		if(exist > 0)
		{
			System.out.println("Java exists in the array.");
		}
		else
		{
			System.out.println("Java does not exist in the array.");
		}
	}
}

Output:

Java exists in the array.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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