CallMeDominic Posted February 9, 2015 Share Posted February 9, 2015 For an array A where: Position A[0] = x Position A[1] = y Position A[2] = z And an empty array B with size 3. Array B after your method: Position B[2] = A[2] Position B[1] = A[1] Position B[0] = A[0] -> B[0] = A[0] = x B[1] = A[1] = y B[2] = B[2] = z A = B Is that actually necessary? I was under the impression that they wanted to generate a path that was simply progressing in the opposite direction. Link to comment Share on other sites More sharing options...
Mysteryy Posted February 9, 2015 Share Posted February 9, 2015 Is that actually necessary? I was under the impression that they wanted to generate a path that was simply progressing in the opposite direction. I don't think you understand what we are saying. Your method literally just returns the exact same order of the path that you pass to it. No change what so ever. Link to comment Share on other sites More sharing options...
CallMeDominic Posted February 9, 2015 Share Posted February 9, 2015 I don't think you understand what we are saying. Your method literally just returns the exact same order of the path that you pass to it. No change what so ever. Huh, looks like I spaced out there. I fixed the method I provided, it works now. Link to comment Share on other sites More sharing options...
Mysteryy Posted February 9, 2015 Share Posted February 9, 2015 Huh, looks like I spaced out there. I fixed the method I provided, it works now. Still is an inefficient way to do it, but it should work now at least. :p Link to comment Share on other sites More sharing options...
CallMeDominic Posted February 9, 2015 Share Posted February 9, 2015 Still is an inefficient way to do it, but it should work now at least. He asked how, not how to do it the most efficiently. Link to comment Share on other sites More sharing options...
FrostBug Posted February 9, 2015 Share Posted February 9, 2015 public <T> void reverse(T[] array) { for(int i = 0; i < array.length >> 1; i++) { T tmp = array; array = array[array.length - 1 - i]; array[array.length - 1 - i] = tmp; } } Link to comment Share on other sites More sharing options...
Botre Posted February 9, 2015 Share Posted February 9, 2015 public <T> void reverse(T[] array) { for(int i = 0; i < array.length >> 1; i++) { T tmp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = tmp; } } Any reason why you use a generic instead of an object parameter? Is it a casting thing? Link to comment Share on other sites More sharing options...
Mysteryy Posted February 9, 2015 Share Posted February 9, 2015 public <T> void reverse(T[] array) { for(int i = 0; i < array.length >> 1; i++) { T tmp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = tmp; } } At least someone gets it :P Any reason why you use a generic instead of an object parameter? Is it a casting thing? Because now the array can be of any type. Link to comment Share on other sites More sharing options...
Joseph Posted February 9, 2015 Share Posted February 9, 2015 (edited) Look into: Collections.reverse() Why not this above? Convert your array into a list. With Arrays#asList(array) then use the Collections#reverse (list) Edited February 9, 2015 by josedpay Link to comment Share on other sites More sharing options...
Mysteryy Posted February 9, 2015 Share Posted February 9, 2015 (edited) Convert your array into a list. If you have to convert it then its not going to be as efficient. i.e. take more memory and time. Edited February 9, 2015 by Mysteryy 1 Link to comment Share on other sites More sharing options...
eleax Posted February 17, 2015 Share Posted February 17, 2015 so If I wanted to walk to some location, and also walk from that location to my starting location, I would need to set up two paths? Link to comment Share on other sites More sharing options...
Ziy Posted February 17, 2015 Share Posted February 17, 2015 so If I wanted to walk to some location, and also walk from that location to my starting location, I would need to set up two paths? Hey man, the reason there was a discussion on reversing a path was that you can use a path to get somewhere and then simply reverse it to return Link to comment Share on other sites More sharing options...