In: Computer Science
Please use java language in an easy way and comment as much as you can! Thanks
(Write a code question) Write a generic method called "findMin" that takes an array of String objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.
public class StringOperation {
public static void main(String[] args) {
//input array
String[] words = { "hello",
"goodbye", "jack", "bye", "yes", "no", "yoo" };
//printing results to console
System.out.println("The shortest word is \"" +
words[findMin(words)] + "\" at index "+findMin(words));
}
// a generic method called "findMin" that takes an
array of String objects as a parameter.
public static int findMin(String words[]) {
//initilize the minString with 0
index
String minString = words[0];
int index = 0;
//traversing array to find min length word
for (int i = 1; i < words.length; i++) {
//if found changeing minString with
that founded string
if (words[i].length() < minString.length()) {
minString = words[i];
index = i;
}
}
// return the index of the smallest value.
return index;
}
}