Java Array

Search Array Program

In this chapter, our task is to write a simple java program:

  • to create and initialize the array
  • take element to be searched
  • search the array to find the element
  • print the index where the element is found and the element itself.

To-Do: Rewrite the above java program and print message "Element not found" only once in case element is not present in array.

Java program to search array

package array;

public class SearchArray {

	public static void main(String[] args) {
		int[] arr = {13, 17, 18, 23, 35, 65};
		//element to search
		int search = 18;
		//search array
		for(int i=0; i<arr.length; i++){
				System.out.println("index "+ i + " contains element "+ search);




index 2 contains element 18



Please Share this page
Views : 52
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!