3

Was asked this during a job interview today. I'm sure it's a pretty simple trick, but I couldn't think of it. How can I traverse a simple Java array from end to beginning (for example, in order to aggregate the sum of all values form right to left), without using the "minus" (-) sign (so no i-- in the loop, or something similar)?

Edit: I'm pretty sure it was supposed to be a trick that doesn't involve Java-specific structures (like Collections). Unfortunately I thought I'd think of it myself later, so I didn't ask what the answer was :/

Cauthon
  • 674
  • 1
  • 7
  • 16
  • 1
    If you are allowed to make use of `Collections`, you can convert the `array ` to `ArrayList`, reverse the list using Collection's `reverse` method and iterate it . – Jimmy Sep 25 '17 at 15:26
  • Possible duplicate of [Iterating through a list in reverse order in java](https://stackoverflow.com/questions/2102499/iterating-through-a-list-in-reverse-order-in-java) – Silvio Mayolo Sep 25 '17 at 15:27
  • Could you give an example of input and output? – Oleg Cherednik Sep 25 '17 at 15:28
  • Possible duplicate of [Can one do a for each loop in java in reverse order?](https://stackoverflow.com/questions/1098117/can-one-do-a-for-each-loop-in-java-in-reverse-order) – Procrastinator Sep 25 '17 at 15:28
  • I'm guessing that if there was an answer without using Java-specific tools like `Collections` someone would have mentioned it. Should I accept the answer? – Cauthon Sep 25 '17 at 15:32

6 Answers6

9

Recursion is an option:

int[] numbers = {0,1,2,3,4,5,6,7,8,9,10};

public void traverseReversed(int[] a) {
    traverseReversed(a, 0);
}

private void traverseReversed(int[] a, int i) {
    if ( i + 1 < a.length ) {
        // Traverse the rest of the array first.
        traverseReversed(a, i+1);
    }
    System.out.println(a[i]);
}

public void test() throws Exception {
    System.out.println("Hello world!");
    traverseReversed(numbers);
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
3

If you are allowed to use Collections you can easily reverse it. Please see the below code -

Collections.reverse(Arrays.asList(array))
Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52
  • 1
    This reverses it in place, which not only destroys the original array's order, but also costs more than simply iterating it in reverse. – Makoto Sep 25 '17 at 15:45
  • Yes you are right. I am new in Java. I thought it is a possible answer, as there is no mention of keeping original order in the OP. – Naseef Chowdhury Sep 25 '17 at 15:53
3

Don't know if the question in the interview is even a bit wise, but this answer will somehow.

Just use the ~ unary bitwise complement operator to obtain your custom -1 :

String[] array = {"aaa","bbb","ccc"};

int minusOne = ~0;// unary bitwise complement, yields -1


for(int i = array.length + minusOne; i >= 0; i = i + minusOne){

    System.out.println(array[i]);
}
Arnaud
  • 17,229
  • 3
  • 31
  • 44
2

If you are allowed to use library like @Naseef mentioned, you can also use Common Lang and reverse array as:

ArrayUtils.reverse(int[] array)
Yogen Rai
  • 2,961
  • 3
  • 25
  • 37
  • This reverses it in place, which not only destroys the original array's order, but also costs more than simply iterating it in reverse. – Makoto Sep 25 '17 at 15:45
2

If you have enough stack space, you can always use recursion, and process the elements on your "way out"

void walkArray(int a[],int i){
  if(i+1<a.length)walkArray(a,i+1);
  System.out.println(a[i]);
}

EDIT, just to make sure: code would be launched as walkArray(a,0)

tevemadar
  • 12,389
  • 3
  • 21
  • 49
1

You can use bitwise arithmetic if you don't want to use recursion. The trick lies in the bitwise not operator. Basically it negates the number and subtracts one from it. So ~(~i+1) is the same as -(-i-1+1)-1 which simplifies to i-1.

int i = array.length;
while(i > 0) {
  i = ~(~i + 1);
  System.out.println(array[i]);
}
kamoroso94
  • 1,713
  • 1
  • 16
  • 19