-1
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.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
kim
  • 29
  • 2
  • HwArray[0]=105 HwArray[1]=151 HwArray[2]=191 HwArray[3]=163 HwArray[4]=186 HwArray[5]=105 HwArray[6]=146 HwArray[7]=141 HwArray[8]=120 HwArray[9]=115 Linear Search Result: -1 – kim Apr 01 '17 at 01:21
  • Possible duplicate of [Reversing elements in Arrays](http://stackoverflow.com/questions/43132149/reversing-elements-in-arrays) – Cardinal System Apr 01 '17 at 01:54

1 Answers1

0

Please research a little more before asking questions, maybe google "java reverse array". That's what I did, and found that this person asked the same thing. Reversing an Array in Java

The solution is simple:

List<Integer> lst = Arrays.asList(list); //Converts the int "list" into a list.
Collections.reverse(lst); //Reverses the list.
result = list.toArray(lst);
Community
  • 1
  • 1
Cardinal System
  • 2,749
  • 3
  • 21
  • 42