LINEAR SEARCH ON ARRAY


public class LinearAdvancedSearch {
//method take to parameter one for array other for key (element want to search on it)
private static void SearchAdvanced(int arrayOfMethod[],int key){    //loop on array elements
for(int indexOfArray=0;indexOfArray<arrayOfMethod.length;indexOfArray++){
//condition to find specific element
if(key==arrayOfMethod[indexOfArray]){
System.out.println("index is :"+indexOfArray);
}//end of if

}//end of for

}//end of method
public static void main(String []args ){

// take object to start access search method
LinearAdvancedSearch ob=new LinearAdvancedSearch();

// let it ur array elements
int sourceArray[]={2,3,4,5,6,7,8,9,10,11,12,45,78,3,45,87,3,5,7,8,4,3};

/*
* import java util scanner to start use scan to call method nextInt() and

* use it to take key (element u want to search on it from user )
*
*/
Scanner scan=new Scanner(System.in);
int keyUserEnterIt=5; // initial value

while(keyUserEnterIt!=0){ // while to ask user many times not do this statement one time and exit
//while stop if user eneter 0
System.out.print("enter ur key");
keyUserEnterIt=scan.nextInt();  //ask user for element which he want to search on array

//call method
ob.SearchAdvanced(sourceArray, keyUserEnterIt);
}//end of while

} //  end of main
}//end of class
Advertisements

One thought on “LINEAR SEARCH ON ARRAY

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s