1. Member
Join Date
Jul 2012
Posts
93
Rep Power
0

## reversing an array

hi all,

I need to reverse an array that does not reverse the offset value at the beginning or the end of the array. If someone can help me clean and fix my code it would be greatly appreciated.
If this is not clear let me know.

Java Code:
import java.util.Arrays;

public class Practice {

public void reverse(int offset) {
System.out.println("offset amount = " + offset);
int temp = 0;
int[] data = {-2,3,5,-14,1,2,3,4,9,7};
System.out.println("data = " + Arrays.toString(data));
if (data.length - 1 - offset == offset - 1);
else if (offset + 1 == data.length - 1 - offset) {
temp = data[offset];
data[offset] = data[data.length - 1 - offset];
data[data.length - 1 - offset] = temp ;
}
else{
for (int i = 0; i < data.length - 1 - offset; i++) {
temp = data[data.length/2 + i];
System.out.println(temp);
data[data.length/2 + i] = data[data.length/2 - i - 1];
data[data.length/2 - i - 1] = temp;
System.out.println(temp);
if (temp == data[data.length - 1 -offset])
break;
}
}
System.out.println("reversed array with null offsets" + Arrays.toString(data));
}

public static void main(String[] args) {
Practice reversePractice = new Practice();
reversePractice.reverse(2);
}
}

2. ## Re: reversing an array

What does this code do? What is the problem?

3. Member
Join Date
Jul 2012
Posts
93
Rep Power
0

## Re: reversing an array

Originally Posted by KevinWorkman
What does this code do? What is the problem?
Thanks,

What I need it to do is take the array data and reverse the values. Say the last number is 7 and the first number is -2, then the first number will be switched to 7 and the last to -2. The catch with my question is to offset the beginning and end of the array with a value (in my case 2 "public void reverse(int offset)" "reversePractice.reverse(2);"). that will reverse the array of whatever size with whatever values starting from 2 over from the left (data[2]) and 2 over from the right data[n - 1 - 2] (data[7] in the array declared in the code above). Note: the code above is not 100% correct.

4. ## Re: reversing an array

What's not correct about it? What exactly does the code do? What is incomplete about it? What do you need help with?

I suggest drawing a bunch of example arrays out on a piece of paper, then drawing out the results of calling this method with different offsets. You should notice a pattern that you can use.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•