Difference between revisions of "Find Element x Using Linear Search"
Line 7: | Line 7: | ||
|SideSection= | |SideSection= | ||
[[Image:Wiki_array03.jpg|center]] | [[Image:Wiki_array03.jpg|center]] | ||
− | |||
|Solution= | |Solution= | ||
Line 16: | Line 15: | ||
} | } | ||
</pre> | </pre> | ||
− | + | ||
+ | We'll want to keep a boolean variable to keep track of whether or not you have found the key value x. At the beginning of your method, initialize this boolean variable to false since you have not yet found the key value. This will also be the value to return at the end of the method. | ||
<pre> | <pre> | ||
boolean exists = false; | boolean exists = false; | ||
Line 22: | Line 22: | ||
return exists; | return exists; | ||
</pre> | </pre> | ||
− | + | ||
+ | Use a for loop to go through each element in the array to check if it is the value we're looking for. | ||
<pre> | <pre> | ||
for(i = 0; i < array.length; i++) { | for(i = 0; i < array.length; i++) { | ||
Line 28: | Line 29: | ||
} | } | ||
</pre> | </pre> | ||
− | |||
− | |||
+ | For each element in the array, use an if statement to compare each element with the given key value. In order to compare each element with the key value, use the '==' operator. If the current element is equal to the key value, that means you have found the element in the array so you can set your boolean variable to true. | ||
+ | <pre> | ||
+ | if(array[i] == x) { | ||
+ | exists = true; | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | Putting everything together, here's what the entire method should look like: | ||
<pre> | <pre> | ||
public static boolean elementExists(int[] array, int x) { | public static boolean elementExists(int[] array, int x) { |
Revision as of 12:49, 6 April 2010
Back to the Program-A-Day homepage
ProblemCreate a method that accepts an array of integers and a key integer value x to determine whether or not the key value exists in the given array. The method should return true if the key value was found in the array, otherwise the method should return false. |
Array Algorithms | |
---|---|---|
SolutionLet's start with creating the skeleton of our method with the proper return type and accepted parameter values. public static boolean elementExists(int[] array, int x) { } We'll want to keep a boolean variable to keep track of whether or not you have found the key value x. At the beginning of your method, initialize this boolean variable to false since you have not yet found the key value. This will also be the value to return at the end of the method. boolean exists = false; return exists; Use a for loop to go through each element in the array to check if it is the value we're looking for. for(i = 0; i < array.length; i++) { } For each element in the array, use an if statement to compare each element with the given key value. In order to compare each element with the key value, use the '==' operator. If the current element is equal to the key value, that means you have found the element in the array so you can set your boolean variable to true. if(array[i] == x) { exists = true; } Putting everything together, here's what the entire method should look like: public static boolean elementExists(int[] array, int x) { int i; // loop iterator boolean exists = false; for(i = 0; i < array.length; i++) { if(array[i] == x) { exists = true; } } return exists; } | ||
Code |