• Post Reply Bookmark Topic Watch Topic
  • New Topic

I can't understand why this code works  RSS feed

 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The method reverseIteration does not take any parameters and it doesn't act on a (which is not even declared as an int[]). This is from a website which explains algorithms. I also tried to create a similar code to reverse an array but it's not working (it gives me the same array unmodified). Here lies my terrible attempt.
How could the code above work is my biggest concern. Mine is defying the law of pass by value somehow (I think).

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:
The method reverseIteration does not take any parameters and it doesn't act on a (which is not even declared as an int[]).

I'm not totally sure what the issue is. The method reverseIteration doesn't take a paramater, but it doesn't need to. Instead it simply uses the the static int[] a on line 2. The static 'a'  is bound to a populated array object in line 26.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Draw the movements of the values on the back of a used envelope.
The index should run only half the range, that is OK so want the reverse the array.
If you run the index through the whole range, you will exchange the elements twice, so the array will end up not modified.
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:
The method reverseIteration does not take any parameters and it doesn't act on a (which is not even declared as an int[]).


Yes t is.
Right there on line 2.

That static variable is set up on line 26, which is the first line of the main() method, then used in the reverse methods.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:The method reverseIteration does not take any parameters and it doesn't act on a (which is not even declared as an int[]).

As Dave said: yes it does. Look a bit more closely.

However, what you may be noticing is the fact that those methods are tightly coupled - specifically, they require a separately defined array (the one defined at line 2) in order to work.

And there's no good reason why they should. For example:does the job just as well, and doesn't rely on an outside variable; and is arguably even better. And the same holds true for reverseRecursive().

FYI:
1. The above logic can be compressed quite a bit by using a for loop instead.
2. The start/end test doesn't need to include '=', because if the array contains an odd number of elements, the one in the middle doesn't need to be moved.
Viz:
HIH

Winston
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did it guys, thanks for your input! 
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:I just did it guys, thanks for your input
Good. As an aside, do you now understand why the code in your opening post works i.e. the 'static int[] a' var being available inside the static method?

Looking at your code I would make a couple of aesthetic comments:
Could be confusing to other people looking at your code to see an object called, err, object. Even just a letter such as 'r' would look better.
'arrayparamter' is very long and makes the method code look clunky. Better with something shorter.

All the best,

PaulC.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:Good. As an aside, do you now understand why the code in your opening post works i.e. the 'static int[] a' var being available inside the static method?

.


Hi, I understand, but I really don't agree with it. I think it's much better to use a parameter, that way each method will have its' own identity.
Thank you for the comments, I will work on renaming variables better.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:Hi, I understand, but I really don't agree with it. I think it's much better to use a parameter, that way each method will have its' own identity
As long as a programmer understands why something does or doesn't work, that's the most important thing. How you eventually implement is up to you.

All the best,

PaulC.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
'arrayparamter' is very long and makes the method code look clunky. Better with something shorter.

I agree that arrayparameter is a poor choice for a name, not because it's long, but because it doesn't describe the parameter well.  I'd be happier with arrayToSort or unsortedArray.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!