PDA

View Full Version : java: why does this segment of code reverse the order of the elements of an array?




macman2790
Feb 14, 2007, 10:27 PM
i'm reviewing for a test and this is about the only thing that's bothering me.
heres the code: //Write a code segment that reverses the elements in the integer array, a.
int temp;
for (int i=0;i<a.length/2;i++)
temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;



iW00t
Feb 14, 2007, 10:54 PM
Yeah it is intriguing...

Why is it looped up to a.length/2?

plinden
Feb 14, 2007, 11:02 PM
Is that your own code or from a teacher or book? Since all that does is set temp, and is equivalent to:
int temp;
for (int i=0;i<a.length/2;i++) {
temp = a[i];
}
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;

In other words, you (or your teacher or text book) will have to be more careful about defining blocks of code.

The following makes more sense with your question:
int temp;
for (int i=0;i<a.length/2;i++) {
temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}

If you don't understand how this works, do it on paper, with the array a going from 0 to 9 so you have the loop going from 0 to 5.

macman2790
Feb 14, 2007, 11:03 PM
Yeah it is intriguing...

Why is it looped up to a.length/2?

no idea, that's one of the things that's killing me, but another part i don't understand is the is the part to the right of it which is that -i -1. yeah it's my teacher, i know, i have to deal with it everyday. i see what you mean about the block

macman2790
Feb 14, 2007, 11:10 PM
Is that your own code or from a teacher or book? Since all that does is set temp, and is equivalent to:
int temp;
for (int i=0;i<a.length/2;i++) {
temp = a[i];
}
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;

In other words, you (or your teacher or text book) will have to be more careful about defining blocks of code.

The following makes more sense with your question:
int temp;
for (int i=0;i<a.length/2;i++) {
temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}

If you don't understand how this works, do it on paper, with the array a going from 0 to 9 so you have the loop going from 0 to 5.
I'm not quite sure how to do it on paper, the only thing i see is the 0 to 5 thing.

plinden
Feb 14, 2007, 11:43 PM
Doing a of length 6 for simplicity, write the following on a piece of paper

start i = 0 i = 1
a[0] 0 5 ?
a[1] 1 1
a[2] 2 2
a[3] 3 3
a[4] 4 4
a[5] 5 0

step 0, i = 0, a.length - i - 1 = 5
-------------------------------------
temp set to a[0] = 0
a[i] is a[0], set to a[5], ie. a[0] = 5
a[a.length - i - 1] is a[5], set to temp, ie. a[5] = 0

step 1, i = 1, a.length - i - 1 = 4
-------------------------------------
temp set to a[1] = 1
etc

Do the rest yourself. Continue the loop to a.length/2 (i.e. 3)

macman2790
Feb 15, 2007, 01:17 AM
Doing a of length 6 for simplicity, write the following on a piece of paper

start i = 0 i = 1
a[0] 0 5 ?
a[1] 1 1
a[2] 2 2
a[3] 3 3
a[4] 4 4
a[5] 5 0

step 0, i = 0, a.length - i - 1 = 5
-------------------------------------
temp set to a[0] = 0
a[i] is a[0], set to a[5], ie. a[0] = 5
a[a.length - i - 1] is a[5], set to temp, ie. a[5] = 0

step 1, i = 1, a.length - i - 1 = 4
-------------------------------------
temp set to a[1] = 1
etc

Do the rest yourself. Continue the loop to a.length/2 (i.e. 3)
thanks, you were a big help.

jeremy.king
Feb 15, 2007, 09:57 AM
plinden - you are thorough!

macman, if you haven't noticed, that algorithm walks through the first half of the array swapping elements.

1st pass - swaps 1st and last element
2nd pass - swaps 2nd and 2nd to last element
and so on.

If you traverse the entire array (instead of only half) you would simply end up with the original array.