Passing "<", ">", "=", etc as an argument to a method?

From: Eric A. Forgy (forgy_at_uiuc.edu)
Date: 11/28/03

  • Next message: Anthony Borla: "Re: Passing "<", ">", "=", etc as an argument to a method?"
    Date: 27 Nov 2003 17:19:40 -0800
    
    

    Hello,

    I am just learning Java and am trying to write a method that does
    something like

    //===========================================
    public Static List find(double[] array,double val,String relationalOp)
    {

       List list = new ArrayList();
       for (int m = 0;m < array.length;m++) {
          // The following line is the problem
          if (array[m] relationalOp val) {
             list.add(array[m]);
          }
       }
       return list;
    }
    //===========================================

    Is it possible to use an argument directly in an if statement like
    that?

    I am trying to avoid having to rewrite the same method over and over
    again, once for each relational operator "==", "<", ">", "<=", ">=",
    not to mention all of their "not" versions.

    What is the best way to do this?

    Thank you for any help.

    Eric


  • Next message: Anthony Borla: "Re: Passing "<", ">", "=", etc as an argument to a method?"