Question
To do's in the code public class ArrayLinearSearch { public static void main(String[] args) { int[] myList = {95, -10, 23, -3, 78}; System.out.print(myList: );
To do's in the code
public class ArrayLinearSearch { public static void main(String[] args) { int[] myList = {95, -10, 23, -3, 78}; System.out.print("myList: "); printArray(myList); //TODO1: Write code to search for the item 23 and print its index in the array //TODO2: Write code to search for the item 90 and print its index in the array. If the item is not in the array. // print -1 as the index } /* TODO3: * (1) * Define a method linearSearch. Given an int array and an int key, * the method searches for the key in the array and returns the * location (index) of the first occurrence of the key. * * Note tha if the key is not in the array, -1 should be returned. * * Examples: * If the list is 10, 40, 20 and 30, and the key is 20, * the method should return 2. * * If the list is 10, 40, 20 and 30, and the key is 50, * the method should return -1. * * (2) * Change the code in the main to method invocations to linearSearch. */
/* * printArray: Given an int array, the method print out all the elements of the array. */ public static void printArray(int[] list) { for (int number: list) { System.out.print(number + " "); } System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started