Home Java Beginners Search an elements in the array
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Search an elements in the array

Advertisement
In this section we will discuss about how to check the availability of an element in the array.

Search an elements in the array

In this section we will discuss about how to check the availability of an element in the array. Array is a collection of similar data type used in most of the programming languages. By the help of this program we are going to check the element in the array whether the elemnt is present or not.. In the program first user enter a size of array, based on the size entered user have to enter the element in the array.  Here is the program to search an element in the array.

import java.util.Scanner;
public class Program 
 {
  public static void main(String args[])
  {
   Scanner in=new Scanner(System.in);
   System.out.println("Enter the limit number");
   int limit = new Scanner(System.in).nextInt();
   int[] a=new int[limit];
    for(int i=0;i<a.length;i++)
    {
     System.out.println("Enter the element");
     //in=new Scanner(System.in); 
     int number=in.nextInt();
     a[i]=number;
    }
    System.out.println("Enter the key element to check availability");
    int key=in.nextInt();
     for(int j=0;j<a.length;j++)
     {
       if(a[j]==key)
       System.out.println(a[j]+" found at location "+j);
      }
   }
 }

Description : In the above program user check the availability of the element in the array. When a user enters and stores an element in the array, program matches it with the key element . If the element is found, it will return the location of the element else will return not found.

Output :  After Compiling and executing the above program

Download SourceCode

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2013

Ask Questions?    Discuss: Search an elements in the array  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com