# Thread: Array reverse algorithim - reversing elements

1. Member
Join Date
Aug 2010
Posts
70
Rep Power
0

## 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 :
Java 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.

}

}```

2. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
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?

3. Imagine what you are doing is swapping the elements in the array; instead of doing this:
Java 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```
You are instead doing this:
Java 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.

4. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
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!)

5. To solve array manipulations, it useful to take a piece of paper and a pencil and work thru the logic before writing the code.

6. Originally Posted by Adomini
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.
Java 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```

Originally Posted by Adomini
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:
Java 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:
Java 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:
Java Code:
```int newInt = z[x] - y; // WRONG!
int newInt = z[x - y]; // RIGHT!```

Hope that puts you on the right track.

7. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
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!

8. Originally Posted by Adomini
I hope I do not wear you guys out with my constant haggling if it comes to that.
It's a forum; we're here to help people who have questions.

9. Member
Join Date
Aug 2010
Posts
3
Rep Power
0
Moderator edit -- hijack post deleted. Poster please see my note below.
Last edited by Fubarable; 08-30-2010 at 04:14 AM.

10. kjv, welcome to the forum, but I ask if you have your own question that you post it in a new thread, one of your very own, rather than hijacking someone else's thread. I have no choice but to delete your question.

#### Posting Permissions

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