CallMeDominic Posted February 9, 2015 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.
Mysteryy Posted February 9, 2015 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.
CallMeDominic Posted February 9, 2015 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.
Mysteryy Posted February 9, 2015 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
CallMeDominic Posted February 9, 2015 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.
FrostBug Posted February 9, 2015 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; } }
Botre Posted February 9, 2015 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?
Mysteryy Posted February 9, 2015 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.
Joseph Posted February 9, 2015 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
Mysteryy Posted February 9, 2015 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
eleax Posted February 17, 2015 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?
Ziy Posted February 17, 2015 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