wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Pass By Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pass By Value" Watch "Pass By Value" New topic
Author

Pass By Value

Marvin Porte
Greenhorn

Joined: Nov 11, 2012
Posts: 11
Hello I'm practicing to code in java and here's one of the problem I'm trying to solve:
Given an array of positive ints, return a new array of length "count" containing the first even numbers from the original array. The original array will contain at least "count" even numbers.

copyEvens({3, 2, 4, 5, 8}, 2) → {2, 4}
copyEvens({3, 2, 4, 5, 8}, 3) → {2, 4, 8}
copyEvens({6, 1, 2, 4, 5, 8}, 3) → {6, 2, 4}

Here is my solution:


Now my problem is this:
1. If I run the code, it returns {0, 0, 0 ... (length)} // I think it has something to do with "pass by value" I think?
2. To solve this number 1 problem, I tried to return the output inside the loop. To do this, I replaced the 'break' by 'return output' and erase the 'return output' outside the loop. But it doesn't compile and here's the error message:
Error: public int[] copyEvens(int[] nums, int count) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This method must return a result of type int[]

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider using a final else {... to ensure that return is always called.

Can you help me improve my code and can you clarify me some rules regarding pass by value, return, and if else statement? I'm confused. Thanks!
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Try this buddy...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Ranjith Suranga wrote:Try this buddy...

@Marvin: Please DontWriteLongLines (←click). It makes your thread very hard to read.

@Ranjith: If you see long lines in a post, please don't copy/quote them.

I've broken both of yours up this time, but please re-read the UseCodeTags page carefully.

Thanks

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Marvin Porte wrote:1. If I run the code, it returns {0, 0, 0 ... (length)} // I think it has something to do with "pass by value" I think?

Nope. Absolutely nothing whatsoever.

It has to do with the way you've written your loop.

Look at it again and ask yourself this: What is 'length' for, and does that gel with how I'm using it?
If you need to, get out a pencil and paper and write down its values for each pass through the loop.

Winston
Marvin Porte
Greenhorn

Joined: Nov 11, 2012
Posts: 11
@winston
oops sorry sir i thought that if i write code here, it will look the same when i submit it.
yeah i get it now. "i<count" is wrong in the code as well as "if(nums[i]/2==0)". must be i<nums.length and % instead of / like what ranjith did. thank you! now the code is working just fine when i test it. =D
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Winston Gutkowski wrote:
Ranjith Suranga wrote:
@Ranjith: If you see long lines in a post, please don't copy/quote them.


Got it.. It is extremely thank you for showing my faults..
 
Consider Paul's rocket mass heater.
 
subject: Pass By Value