public static void main(String[] args) {
int[] HwArray = new int[10];
int count = 0;
String separate = "";
for (int i = 0; i < HwArray.length; i++) {
System.out.print(separate);
//Generate random numbers
HwArray[i] = (int) (100 + Math.random() * 100);
System.out.println("HwArray[" + i + "]=" + HwArray[i]);
}
int location = linearSearch(HwArray, 150);
System.out.println("\nLinear Search Result: " + location);
}
// Reverse the order of all elements, then print HwArray.
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
result[j] = list[i];
System.out.println("HwArray[" + i + "]=" + result[j]);
}
return result;
}
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++) {
if (list[i] == key)
return i; //return the index location
}
return -1; //return if number is not found in the index
}
I'm trying to print out the elements in Reverse, but It will only print out the elements. I'm not sure what's wrong.