Revise LinearSearch. java listing 7.6, page 266 (name it LinearSearch2), to retu
ID: 3853586 • Letter: R
Question
Revise LinearSearch. java listing 7.6, page 266 (name it LinearSearch2), to return the indexes of all duplicated target keys. Design the main method of your program such that it allows the user to re-run the program with different sets of inputs (i.e., use a loop). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable. public class LinearSearch { /** The method for finding a key in the list */ public static int linearSearch(int[] list, int key) { for (int i = 0: iExplanation / Answer
package com.java2novice.algos;
public class MyLinearSearch {
public static int linerSearch(int[] arr, int key){
int size = arr.length;
for(int i=0;i<size;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
public static void main(String a[]){
int counter, num, searchkey , array[];
//To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
//Creating array to store the all the numbers
array = new int[num];
System.out.println("Enter " + num + " integers");
//Loop to store each numbers in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();
System.out.println("Enter the search value:");
searchkey = input.nextInt();
System.out.println("Key "+searchKey+" found at index: "+linerSearch(array, searchKey));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.