0
public static void main(String[] args) {

    int[] HwArray = new int[10];
    for (int i = 0; i < HwArray.length; i++) {
        HwArray[i] = i;
        }

    int count = 0;
    for (int i = 0; i < HwArray.length; i++){
    HwArray[i] = (int) (100 + Math.random() * 100);         

    System.out.print("HwArray[i]=" + HwArray[i]);
            }

        }

    {
        int[] reverse(int[] HwArray); {
            int[] reversed = new int[HwArray.length];
            for (int i=0; i<HwArray.length; i++) {
                reversed[i] = HwArray[HwArray.length - 1 - i];
            }
            return reverse;
        }
    }

}

Sorry, I'm still learning. I'm trying to reverse the order of all the elements, but I keep receiving an error. Am I doing something wrong?

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
kim
  • 29
  • 2

4 Answers4

0

I think the conceptually easiest way to reverse the order of elements in an array is to swap each ith element in the first half of the array of size N with the N-ith element in the second half:

int[] reversed = new int[10];
for (int i=0; i < HwArray.length/2; ++i) {
    reversed[i] = HwArray[HwArray.length-1-i];
    reversed[HwArray.length-1-i] = HwArray[i];
}

Demo here:

IDEOne

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

There are many errors:

First of all, it should be return reversed instead of return reverse.

Then, you cannot define any method inside a method(here, method reverse is defined inside the main method)

Then, you can do one thing, remove the following two braces: 1: The opening brace just before int[] reverse(int[] HwArray) 2: The closing brace in the last line

Last, it should be int[] reverse(int[] HwArray) { instead of int[] reverse(int[] HwArray); {

Deepesh Choudhary
  • 660
  • 1
  • 8
  • 17
0
Comparator<Integer> comparator = new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
};

// option 1
Integer[] array = new Integer[] { 11, 44, 4, 3, 123 };
Arrays.sort(array, comparator);

// option 2
int[] array2 = new int[] {11, 44, 4, 3, 123};
List<Integer>list = Ints.asList(array2);
Collections.sort(list, comparator);
array2 = Ints.toArray(list);

// option 3
List<Integer> integersList = Ints.asList(arr);
Collections.sort(integersList, Collections.reverseOrder());
vlaxmi
  • 468
  • 4
  • 18
0

To be honest, you're actually not that far off. The reverse function actually seems to work okay but you have all kinds of weird syntax errors and you're not even calling the reverse method. Try doing this:

  1. Move the reverse method inside the class where main is defined. If you want to call it directly from main you'll have to make it an static method.

  2. Get rid of the extra curly braces on lines 17 and 25.

  3. Remove the semicolon on line 18 when declaring reverse. It shouldn't be there.

  4. The reverse method is trying to return a variable called reverse. That's the name of the method, you can't do that. I think you meant to return reversed.

  5. Actually call the reverse method after you have initialized the array with random numbers. Then print the array out again to verify that it worked.

  6. Notice that you're not actually printing out the values of the array on line 11. That should be System.out.println("HwArray[" + i + "]=" + HwArray[i]);

d512
  • 32,267
  • 28
  • 81
  • 107