We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › max() in 0125 not working
Page Index Toggle Pages: 1
max() in 0125 not working (Read 601 times)
max() in 0125 not working
Jul 16th, 2007, 9:46pm
 
public float max(float[] array) {
float retVal = -Float.MAX_VALUE;

for(int i = 0; i < array.length; i++) {
if(array[i] > retVal) {
retVal = array[i];
}
}

return retVal;
}

in 0124 can run,but in 0125
Semantic Error: The instance method "float max(float[] array);" cannot override the accessible static method "float max(float[] $1);" declared in type "processing.core.PApplet".

Re: max() in 0125 not working
Reply #1 - Jul 16th, 2007, 10:10pm
 
float[] array;
public float max() {
float retVal = -Float.MAX_VALUE;

for(int i = 0; i < array.length; i++) {
if(array[i] > retVal) {
retVal = array[i];
}
}

return retVal;
}

run!run!run!
Re: max() in 0125 not working
Reply #2 - Jul 16th, 2007, 10:31pm
 
Check out http://processing.org/download/revisions.txt - max() has been added for float and int arrays, so you should not need to implement these yourself anymore.  You're getting an error because you can't override a static method (though see http://faq.javaranch.com/view?OverridingVsHiding for more discussion on this - long story short, if you declare your new method static you can hide the old one, but it's not quite the same as overriding it).  The implementation in 0125 is as follows:

Code:

static public final float max(float[] list) {
if (list.length == 0) {
return Float.NaN;
}
float max = list[0];
for (int i = 1; i < list.length; i++) {
if (list[i] > max) max = list[i];
}
return max;
}


This should be doing what you want, as long as you realize that it sends NaN if the array is empty rather than MAX_VALUE, so you should be all set.
Re: max() in 0125 not working
Reply #3 - Jul 16th, 2007, 10:58pm
 
float[] list;
public final float max() {
        if (list.length == 0) {
        return Float.NaN;
      }
        float max = list[0];
        for (int i = 1; i < list.length; i++) {
        if (list[i] > max) max = list[i];
      }
      return max;
 }

it can run
Page Index Toggle Pages: 1