# Array reverse algorithim - reversing elements

• 08-29-2010, 10:14 PM
Array reverse algorithim - reversing elements
Hi everyone. Thanks in advance for everyone who created this forum!

I am a student in a java learners class, and I wanted some feedback on some work that I am doing in school. (not the answer, but some pointers)

The homework is I have to construct a method that will reverse the elements in an array of integers. For efficiency purposes, this method will not create another array in the process of the reversing.

This is my code so far, but my issue is that while I am using a " for " loop for counting int [] a backwards, I am not initializing the elements.....

Code so far is :
Code:

```         public static void reverse(int[] a)         {                                 if (a == null)                         throw new IllegalArgumentException("null array");                                                 for( int i = a.length; i > 0 ; i --)                                 {                          // this is the tough part of the code,                         // as I cannot create another array to put                         //the reverse order in.                                                         }                     }```
• 08-29-2010, 10:20 PM
I have tried to put in the for loop a[i], but an error comes up.

Since I cannot return anything, (which is why method is void), I am confused immensely.

It has to have a[i] in the for loop somewhere, right?
• 08-29-2010, 10:30 PM
Zack
Imagine what you are doing is swapping the elements in the array; instead of doing this:
Code:

```Array A:        Array B: 1 2 3 4 5 6        6 1 2 3 4 5 6        6 5 1 2 3 4 5 6        6 5 4 1 2 3 4 5 6        6 5 4 3 1 2 3 4 5 6        6 5 4 3 2 1 2 3 4 5 6        6 5 4 3 2 1```
Code:

```Array A: 1 2 3 4 5 6 6 2 3 4 5 1 6 5 3 4 2 1 6 5 4 3 2 1```
So to start, you swap 1 and 6, then 2 and 5, then 3 and 4, then terminate the loop.

Hopefully this helps a bit; you will need to access a[i], yes, and another element of a as well.

I'm not sure why you're not allowed to return anything, but that's another issue altogether. For now we'll stick to working on the loop.
• 08-29-2010, 10:49 PM
Thanks Zach. By another element in the loop, I am assuming then that I should use another counter, like "j" instead of "i" , right? That is the way how I can identify two elements at once (such as '6' and '1' , then '2' and '5', etc....)

I tried the answer like a[i] = (a[a.length] - 1) -i), and it ran, but my professor said that while it may have worked in this instance, it is not correct. (sadness!)
• 08-29-2010, 11:19 PM
Norm
To solve array manipulations, it useful to take a piece of paper and a pencil and work thru the logic before writing the code.
• 08-29-2010, 11:43 PM
Zack
Quote:

Thanks Zach. By another element in the loop, I am assuming then that I should use another counter, like "j" instead of "i" , right? That is the way how I can identify two elements at once (such as '6' and '1' , then '2' and '5', etc....)

I don't see why you wold use two different variables... think about it; as you get further from one end, you get the same distance from the other end also. As Norm suggested, you can work this out on paper and see that, to achieve the desired result, you only need the one variable.
Code:

```[color=red]1[/color] 2 3 4 5 [color=red]6[/color] 6 [color=red]2[/color] 3 4 [color=red]5[/color] 1 6 5 [color=red]3[/color] [color=red]4[/color] 2 1 6 5 4 3 2 1```

Quote:

I tried the answer like a[i] = (a[a.length] - 1) -i), and it ran, but my professor said that while it may have worked in this instance, it is not correct. (sadness!)

Your code is very close to being correct. Not sure why your professor didn't say that you were on the right track. But think about what your code does:
Code:

`([color=red]a[a.length][/color] - 1) -i)`
This will produce an exception. I'm assuming you know that arrays are zero based: The "0" element of {6,5,4,3,2,1} is 6. The "5" element of {6,5,4,3,2,1} is 1. Since the length is 6, we're looking for the "6" element... which doesn't exist.

Instead, you need to think about which element you should be accessing--when choosing that element, its value should be inside the [] entirely. So, in some situation where you had:
Code:

```int x = 5; int y = 2; int[] z = {1,2,3,4,5,6};```
And you wanted to pick the x-y'th element of the array:
Code:

```int newInt = z[x] - y; // WRONG! int newInt = z[x - y]; // RIGHT!```

Hope that puts you on the right track.
• 08-30-2010, 02:42 AM
Got it! I also went to a friend who gave me the same explanation and broke it down for me. You guys are awesome. Thanks, really. (So mad at my professor........)

I will check back in for more help when I get problems that I cannot solve in 24 hours or so. I hope I do not wear you guys out with my constant haggling if it comes to that.

Thanks so much!
• 08-30-2010, 03:43 AM
Zack
Quote: