-1

I would like to write a concise code that reverse an array of ints without using any loops. By "reversing" I mean the following: [1, 4, 3, 7, 2] -> [2, 7, 3, 4, 1]. I think that IntStream could be helpful here: it should be possible to "catch" a finite stream and proceed with it like with a stack (LIFO). Unfortunately I can't - could you please help?

Almost sure
  • 145
  • 5
  • 8
    NOTE IntStream uses a loop. I think you mean "reversing" and this type of homework is typically solved using recursion. I suggest you try that and ask another question if you get an issue. – Peter Lawrey May 02 '19 at 15:23
  • I've corrected revert into reverse. By recursion you mean looping, don't you? Well, loops are the most frequently repeated fragments of my code so I am trying to find ways of removing them - it is as simple as that. The above exercise is easy with a loop, but I am not able to solve it without it. It's a pity that you can't help :( – Almost sure May 02 '19 at 15:29
  • 2
    "By recursion you mean looping, don't you?" I am certain that Peter knows the difference. – Andy Turner May 02 '19 at 15:30
  • 1
    No, recursion is very specifically an alternative to looping. – Peter Lawrey May 02 '19 at 15:32
  • Thanks for answer but still recursion is not what I'm searching for. I'm searching for something analogous to the following way of finding a harmonic mean for ints from ```inputArray```: ```Math.exp(IntStream.of(inputArray).mapToDouble(x->Math.log(x)).sum()/inputArray.length)``` – Almost sure May 02 '19 at 15:37
  • 1
    Since you are ok with IntStream, is [this](https://stackoverflow.com/a/46756353/9223839) what you want? – Joakim Danielson May 02 '19 at 15:43
  • Thank you cordially Joakim Danielson, this is what I was searching for :) – Almost sure May 02 '19 at 15:46

2 Answers2

1

Any loop can be replaced with recursion.

Hire is an example.

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class RecursionReverseArray {


    private static <T> void reverseImpl(T[] arr,int lhsIdx, int rhsIdx) {
        if(lhsIdx == rhsIdx)
            return;
        T tmp = arr[lhsIdx];
        arr[lhsIdx] = arr[rhsIdx];
        arr[rhsIdx] = tmp;
        reverceImpl(arr, ++lhsIdx, --rhsIdx);
    }

    public static <T> void reverse(T[] arr) {
        reverseImpl(arr, 0, arr.length-1);
    }

    @Test
    void test() {
        Integer[] actual = {9,8,7,6,5,4,3,2,1};
        Integer[] expected = {1,2,3,4,5,6,7,8,9};
        reverse(actual);
        assertArrayEquals(expected, actual);
    }

} 

As well as with Java 8+ you can use reverse order stream API.

Victor Gubin
  • 2,782
  • 10
  • 24
0

If you don't want to use loops, you can use recursions if you want. Here is a code written using recursion.

CODE

import java.io.*;

class ReverseArray {

static void rvereseArray(int arr[], int start, int end) 
{ 
    int temp; 
    if (start >= end) 
        return; 
    temp = arr[start]; 
    arr[start] = arr[end]; 
    arr[end] = temp; 
    rvereseArray(arr, start+1, end-1); 
} 

static void printArray(int arr[], int size) 
{ 
    for (int i=0; i < size; i++) 
        System.out.print(arr[i] + " "); 
    System.out.println(""); 
} 

public static void main (String[] args) { 
    int arr[] = {1, 2, 3, 4, 5, 6}; 
    printArray(arr, 6); 
    rvereseArray(arr, 0, 5); 
    System.out.println("Reversed array is "); 
    printArray(arr, 6); 
} 

}

Community
  • 1
  • 1