• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Help

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

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.)
  •  
    Ranch Hand
    Posts: 490
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Raymond Xu
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Darn, I should have caught those errors. Thank you all for the help!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic