aspose file tools*
The moose likes Beginning Java and the fly likes loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loops" Watch "loops" New topic
Author

loops

winny dhar
Greenhorn

Joined: Nov 14, 2011
Posts: 13
hello everyone..
i am getting a problem in writing a program to reverse the element of an array using one loop only..so can any one help me out???
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Look at this . Rob explained well there
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Compute the midpoint of the array.
Loop number of times equal to midpoint + 1.

Maintain two counters:
1) One counting up from 0 to midpoint.
2) One counting down from last index to midpoint.

Swap the element at the first counter with the element at the second counter

If first counter == second counter, break out of the loop.


Clivant Yeo
My Personal Website
winny dhar
Greenhorn

Joined: Nov 14, 2011
Posts: 13
thanks ..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Clivant Yeo wrote:. . .
Loop number of times equal to midpoint + 1.

Maintain two counters:
. . .

If first counter == second counter, break out of the loop.

Why + 1? That looks like a mistake.
You don’t need two counters. Only one.
If you use the correct values to count to, there is no need for the break. In fact, using break might introduce an out-by-one error, but I am not certain on that point. Depends how you use it.
sagar kumar nerella
Greenhorn

Joined: Jul 09, 2011
Posts: 18

hello
if i understood your problem correctly
you want some code demo showing the how to reverse the elements of an array using only one loop.

here i am using a temporary array also.

assume your main array of type int and referenced with name mainarray



i hope it will help u.

if i understood your problem wrongly ignore this


sagar kumar nerella,
java certified professional
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sagar kumar nerella wrote:hello
if i understood your problem correctly
you want some code demo showing the how to reverse the elements of an array using only one loop.


Sagar, please remember, this site is NotACodeMill. Simply handing somebody the code does not help that person to learn.
sagar kumar nerella
Greenhorn

Joined: Jul 09, 2011
Posts: 18

sorry sir
for doing that i will not do it again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Apology accepted, but in line with what it says on the title page:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers
... I shall delete the answer. There are better solutions, I believe.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Campbell Ritchie wrote:You don’t need two counters. Only one.

You don't need two, but with one you need to calculate the second index each time. With a counter you don't. In the end this is micro optimization, but I too prefer the two counters. The loop then ends when the two are equal.

If you use the correct values to count to, there is no need for the break. In fact, using break might introduce an out-by-one error, but I am not certain on that point. Depends how you use it.

If you break at the exact center that doesn't matter. If you swap the center element with itself or not will give the same result.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I like to count from 0 to < length / 2. If it has an odd number of elements, you miss the middle element. If it has an even number, you stop at the middle. You do have to swap with array[length - i - 1], however.

If you find that difficult to envisage, work it out with a pencil and paper.
Start by writing a swapElements method. Something like thisYou should have that sort of method somewhere anyway, for general use for swapping pairs in arrays.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
What I meant about break; is that you don’t need it. In the format of loop I usually write, it readsand if you use two indicesSo you don’t need the break; statement at all.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

You're right about not needing the break. I usually use the second form in that exact same format.
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Campbell Ritchie wrote:
Clivant Yeo wrote:. . .
Loop number of times equal to midpoint + 1.

Maintain two counters:
. . .

If first counter == second counter, break out of the loop.

Why + 1? That looks like a mistake.
You don’t need two counters. Only one.
If you use the correct values to count to, there is no need for the break. In fact, using break might introduce an out-by-one error, but I am not certain on that point. Depends how you use it.


Hi Campbell,

You are right about the midpoint, didn't gave much thoughts. Hmm, I am suggesting to use two counters because I am thinking in the direction of an in-place reverse algorithm.

Thanks for highlighting the mistakes!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loops