This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I'm an intro student to Java at UMass and I'm having trouble decoding Array methods. Any help would be appreciated!!
First Method is suppost to take an integer array as an input parameter and check to see if the array contains more zeros than non-zero values:
Second Method is suppost to take an integer array and return a new integer array in which all negative entries in the input array have been changed to zero:
EDIT by mw: Added Code Tags. [ November 11, 2007: Message edited by: marc weber ]
What problems are you having with this? How are you testing this?
Your code is very close to working. I would point out two things:
Your variables "count1" and "count2" are defined within the body of a for loop. They have no meaning outside of that scope.
You are returning the array reference correctly -- it's just not in the correct place. (Hint: You want to iterate through the entire array before returning.)
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Second Method is suppost to take an integer array and return a new integer array in which all negative entries in the input array have been changed to zero: public int[] negToZero (int[] theArray) { for(int j=0; j<theArray.length; j++){ if(theArray[j]<0){ theArray[j] = 0; } return theArray; //how do I return an Array?? } }
Well that method doesn't return a new array, but the old one, with negative values replaced with 0.
That is exactly how you return an array.
What specific questions do you have?
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Raymond Xu
Greenhorn
Joined: Nov 11, 2007
Posts: 8
posted
0
Darn, I should have caught those errors. Thank you all for the help!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.