How to create and access an array of FloatLists

edited September 2014 in How To...

I am looking for a way to sort and store a lot of float numbers in different categories. I want to be able to append numbers to any category at any time. I know how to create a 2D float array, but not how to create and access an array of FloatLists. All help appreciated!

Answers

  • FloatList[] array = new FloatList[number_of_elements];

  • edited September 2014 Answer ✓

    A more complete example. Including populate, sort styles and even append for a FloatList[]: o->

    /**
     * Populate & Sort Array of FloatList (v1.6)
     * by GoToLoop (2014/Sep)
     *
     * forum.processing.org/two/discussion/7258/
     * how-to-create-and-access-an-array-of-floatlists
     */
    
    static final int CATEGORIES = 5, MAX_QTY = 6, MAX_VAL = 100;
    final FloatList[] categories = new FloatList[CATEGORIES];
    
    void setup() {  // replace println() w/ printArray() for newer Processing versions!
      println("All categories' randomly-picked float values:");
      println( populateCategories(MAX_QTY, MAX_VAL, categories) );
    
      // ascend-sort whole categories array:
      println("\nAll categories again. But now after sort():");
      println( sortCategories(false, categories) );
    
      // descend-sort categories' last index only:
      println("\nReverse-sorted last category index #" + (categories.length-1) + ":");
      println( sortCategories(true, categories[CATEGORIES-1]) );
    
      // Add 3 new float values to categories' last index:
      println("\nSame last category w/ 3 new float values appended:");
      println( appendToCategory(categories, CATEGORIES-1, PI, EPSILON, MIN_FLOAT) );
    
      exit();
    }
    
    static final FloatList populateCategories(int maxQty, int maxVal, FloatList... arr)[] {
      for (int i = arr.length; i-- != 0;) {
        int qty = (int) (Math.random()*maxQty) + 1;
        FloatList fl = arr[i] = new FloatList(qty);
    
        while (qty-- != 0) {
          float pick = (float) Math.random()*maxVal;
          fl.append(pick);
        }
      }
    
      return arr;
    }
    
    static final FloatList sortCategories(boolean isReverse, FloatList... arr)[] {
      if (isReverse)  for (FloatList fl: arr)  fl.sortReverse();
      else            for (FloatList fl: arr)  fl.sort();
    
      return arr;
    }
    
    static final FloatList appendToCategory(FloatList[] arr, int idx, float... vals) {
      return appendToFloatList(arr[idx], vals);
    }
    
    static final FloatList appendToFloatList(FloatList fl, float... vals) {
      fl.append(vals);
      return fl;
    }
    
  • To access static methods from another class means they have to be prepended with the class name and in Processing that is the name of the sketch. Which means the code breaks if you use File | Save As

    I can't see a good to declare the methods static in this example?

  • edited September 2014

    Which means the code breaks if you use "File | Save As".
    I can't see any good to declare the methods static in this example?

    And I can't see how a simple static would break any "File | Save As" operation! Is that seriously true? 8-}

    For me, static just means that a class member isn't bound to other instance members!
    And that it's merely a utility method which get everything from its parameters or it's some constant field.
    And that I can move it to any other class, as long as everything it needs is properly imported too!

    For example, all those 5 utility static methods depend on Processing's FloatList class.
    If I wish to copy & paste them somewhere else, I'd need import processing.data.FloatList; there too.
    AFAIK, that's the only demand in order to save them to some ".java" file! 8-|

    To access static methods from another class means they have to be prepended with the class name.
    And in Processing that is the name of the "sketch".

    Well, at least, static opens up that possibility! As long as we know the name of its 1st ".pde" tab! :))
    But that is true for any regular class too! In order to access 1, we gotta know its ".java" file name after all! 8-X

    In short, I only plaster static on class members which somewhat work independent from it! >-)
    If some1 doesn't like them, it's enough to use an editor's replace feature for all static keywords! O:-)

  • edited September 2014

    I can't see how a simple static would break any "File | Save As" operation! Is that seriously true? 8-}

    I thought I made my meaning clear but just to clarify...

    If you have a Processing sketch with static methods in the pde tab and you want to use them from another class (java tab) then they have to be prepended with the name of the sketch. If some time later you rename the sketch with File | Save As then the sketch will not run.

    In a Processing sketch without java tabs then there is no need or benefit to using the static keyword (#) and for a newbie it just introduces a new conundrum for the OP to puzzle over.

    (#) not quite true I can think of one reason to use the static keyword but a novice is unlikely to need it.

    If some1 doesn't like them, it's enough to use an editor's replace feature for all static keywords!

    True - but then the person using the methods must know enough that, in this case, they can safely remove the static keyword. It is more likely that a newbie would assume they are required.

    Apart from that I am sure the OP will find your methods useful :)

  • edited September 2014

    ... and you want to use them from another class (java tab) then they have to be prepended with the name of the sketch.

    IMO, if a Processing programmer knows how to make a ".java" tab; and even more spectacularly, knows how to convert Processing's syntax sugar to proper Java, he's at least intermediary, not a Java rookie! >:/

    And I'd be impressed if a regular Processing's programmer knew that we could access sketch's static members from a ".java" file's top-class by using the 1st ".pde" tab's name as a class type via the dot . operator! O:-)

    It's unknown to me whether Processing's site even teaches that all ".pde" files are merged as 1 to become a full-fledged Java top-class. I've yet to see any mention there's a pre-processor to transpile its syntax sugar to Java!
    They don't even mention that our classes are a special case called nested inner class!

    Your worries seem overrated to me I'm afraid! Looking ahead for a very improbable ".pde" file rename bug case! :-\"
    It's as if they'd be tempting us: "I'm here in a ".pde" file! Plz use me from inside this remotely ".java" expert tab!" :D

  • edited September 2014

    Here is my take on GoToLoop's excellent example

    /**
     * Populate & Sort Array of FloatList (v1.6.1)
     * by GoToLoop (2014/Sep)
     * mod Quark (2014/Sep)
     *
     * forum.processing.org/two/discussion/7258/
     * how-to-create-and-access-an-array-of-floatlists
     */
    
    static final int CATEGORIES = 5, MAX_QTY = 6, MAX_VAL = 100;
    final FloatList[] categories = new FloatList[CATEGORIES];
    
    void setup() {  // replace println() w/ printArray() for newer Processing versions!
      println("All categories' randomly-picked float values:");
      populateCategories(MAX_QTY, MAX_VAL, categories);
      println(categories);
    
      // ascend-sort whole categories array:
      println("\nAll categories again. But now after sort():");
      sortCategories(false, categories);
      println(categories);
    
      // descend-sort categories' last index only:
      println("\nReverse-sorted last category index #" + (categories.length-1) + ":");
      sortCategories(true, categories[CATEGORIES-1]);
      println(categories[CATEGORIES-1]);
    
      // Add 3 new float values to categories' last index:
      println("\nSame last category w/ 3 new float values appended:");
      appendToCategory(categories, CATEGORIES-1, PI, EPSILON, MIN_FLOAT);
      println(categories);
    
      exit();
    }
    
    void populateCategories(int maxQty, int maxVal, FloatList... arr) {
      for (int i = arr.length; i-- != 0; ) {
        int qty = (int) (Math.random()*maxQty) + 1;
        FloatList fl = arr[i] = new FloatList(qty);
    
        while (qty-- != 0) {
          float pick = (float) Math.random()*maxVal;
          fl.append(pick);
        }
      }
    }
    
    void sortCategories(boolean isReverse, FloatList... arr) {
      if (isReverse)  
        for (FloatList fl : arr)  fl.sortReverse();
      else            
        for (FloatList fl : arr)  fl.sort();
    }
    
    void appendToCategory(FloatList[] arr, int idx, float... vals) {
      appendToFloatList(arr[idx], vals);
    }
    
    void appendToFloatList(FloatList fl, float... vals) {
      fl.append(vals);
    }
    
  • edited September 2014

    Since you've removed all static, among other things too, no need for a plain Math.random() anymore!
    You can go back to Processing's own float & non-static random() version now: :ar!

    int qty = (int) random(maxQty) + 1;
    float pick = random(maxVal);
    
  • Thank y’all for your contributions! Here is a compressed code example that might help other novices in the same situation:

    int numberofcategories = 5;
    FloatList[] categories = new FloatList[numberofcategories];
    
    void setup() {  
    
      for (int i = 0; i < categories.length; i++) {
        // Create FLoatlist for each category
        FloatList fl = categories[i] = new FloatList();
        // Append number in floatlist
        fl.append(random(10));
      }
    }
    
  • Here's the same snippet, but calling the original populateCategories(): (*)

    static final int CATEGORIES = 5;
    final FloatList[] categories = new FloatList[CATEGORIES];
    
    void setup() {
      println( populateCategories(1, 10, categories) );
      exit();
    }
    
    static final FloatList populateCategories(int maxQty, int maxVal, FloatList... arr)[] {
      for (int i = arr.length; i-- != 0;) {
        int qty = (int) (Math.random()*maxQty) + 1;
        FloatList fl = arr[i] = new FloatList(qty);
    
        while (qty-- != 0) {
          float pick = (float) Math.random()*maxVal;
          fl.append(pick);
        }
      }
    
      return arr;
    }
    
Sign In or Register to comment.