Access separate lists in 2D array

Hello, I am stumped. This should be simple. I am trying to access the entire sublists/shortlists (not sure what they are called) that make up my 2D array. All of the reference I have seen shows me how to simply select/ print row/column items from the array. Thanks

int brobes[][]  = {{0, 1, 0}, {4, 4, 4}, {4, 5, 5}, {6, 7, 8}};

  int my0[] = brobes[0];
  println(brobes[0]);
  //hoping it will print: {0,1,0} and so forth

Answers

  • i think println has been overridden to print arrays like that.

    i tried println("" + brobes[0]); (converting it to a string first) but that's not right either.

    System.out.println(brobes[0]); didn't work either.

    you'll need to write your own method to display arrays.

  • hmmm... well my goal is to actually access each sublist. Still, should I write a method?

  • Answer ✓

    brobes[0] is the first 'sublist', it's just that it displays 'incorrectly'

  • this will format it how you want it

    void print(int[] array) {
      StringBuilder s = new StringBuilder("{");
      for (int i : array) {
        if (s.length() != 1) { // only the first {?
          s.append(", ");
        }
        s.append(i);
      }
      s.append("}");
      println(s.toString());
    }
    
  • edited June 2016

    How about a more modern way to stringify arrays w/ Java 8's newest StringJoiner class: :)>-
    http://docs.Oracle.com/javase/8/docs/api/java/util/StringJoiner.html

    /**
     * StringifyArray (v1.0)
     * GoToLoop (2016-Jun-23)
     *
     * forum.Processing.org/two/discussion/17283/
     * access-separate-lists-in-2d-array#Item_5
     */
    
    import java.util.StringJoiner;
    
    static final String DELIM = ", ";
    static final String PREFIX = "{", SUFFIX = "}";
    
    @ SafeVarargs static final String stringifyArray(final int... arr) {
      if (arr == null)  return "";
      final StringJoiner sj = new StringJoiner(DELIM, PREFIX, SUFFIX);
      for (final String elem : str(arr))  sj.add(elem);
      return sj.toString();
    }
    
    final int[][] brobes = { {0, 1, 0}, {4, 4, 4}, {4, 5, 5}, {6, 7, 8} };
    
    void setup() {
      for (final int[] subArr : brobes)  println(stringifyArray(subArr));
      exit();
    }
    
  • Great thanks! This very educational.

  • edited June 2016

    So, the end goal has been to define the 2D array and then run through my elementary CA script and choose a different ruleset each time. I am struggling, but I think I am close. Any insight would be appreciated. I am stuck on how to insert a new rule after my end condition is met.

    CA ca;
    
    int delay = 0;
    
    int [][] rulesetIteration = { {0, 0, 0, 1, 1, 1, 1, 0 }, {0, 1, 1, 1, 1, 1, 1, 0}, 
      {0, 0, 1, 1, 0, 1, 1, 0}, {1, 0, 0, 1, 0, 1, 1, 0} };
    
    void setup() {
      size(800, 400);
      background(255);
      ca = new CA(rulesetIteration);
      frameRate(30);
    }
    
    void draw() {
      ca.display(); //draw CA
      ca.generate();
    
      if (ca.finished()) { //clear the screen and iterate through a new ruleSet
        delay++;
        if (delay > 30) {
          background(255);
          ca.chooseNextRule();
          ca.restart();
          delay = 0;
        }
      }
    }
    
    void mousePressed() {
      background(255);
      ca.chooseNextRule();
      ca.restart();
    }
    
    class CA {
    
    
      int [] cells;
      int generation;
    
      int [] ruleset;
      int [][] rulesetIteration;
    
    
      int w = 5; //width and height of cell
    
    
      CA(int[][] rr) { //add new ruleset 
        rulesetIteration = rr;
        //ruleset = r;
        cells = new int [width/w]; //divide screen into cells
        restart();
      }
    
    
      //attempting to choose the next rule with either iteration or through randomness
      void chooseNextRule() {
        for (int i = 0; i < 4; i++) {
          rulesetIteration[i] = int(random(3)); //getting error
        }
      }
    
      //restart to generation 0
      void restart() {
        for (int i = 0; i < cells.length; i++) {
          cells[i] = 0;
        }
        cells[cells.length/2] = 1;
        generation = 0;
      }
    
      //create new generation
      void generate() {
        int[] nextGen = new int[cells.length];
        for (int i = 1; i < cells.length-1; i++) { //remove the edges
          int left = cells[i-1];
          int center = cells[i];
          int right = cells[i+1];
          nextGen[i] = rules(left, center, right);
        }
        cells = nextGen;
        generation++;
      }
    
      //draw black or white cells
      void display() {
        for (int i = 0; i < cells.length; i++) {
          if (cells[i] == 1) fill(0);
          else               fill(255);
          noStroke();
          rect(i*w, generation*w, w, w);
        }
      }
    
      int rules (int a, int b, int c) {
        if (a == 1 && b==1 && c==1) return ruleset[0];
        if (a == 1 && b == 1 && c == 0) return ruleset[1];
        if (a == 1 && b == 0 && c == 1) return ruleset[2];
        if (a == 1 && b == 0 && c == 0) return ruleset[3];
        if (a == 0 && b == 1 && c == 1) return ruleset[4];
        if (a == 0 && b == 1 && c == 0) return ruleset[5];
        if (a == 0 && b == 0 && c == 1) return ruleset[6];
        if (a == 0 && b == 0 && c == 0) return ruleset[7];
        return 0;
      }
    
      boolean finished() {
        if (generation > height/w) {
          return true;
        } else {
          return false;
        }
      }
    }
    
  • Oh man.....

  • There are 2 Tutorials on Arrays - have a look on processing.org

  • Does the first generation work?

    I mean line 87 gives you only a line of rects per generation (whereas the classic game of life gives you a grid for each Generation) nvm

  • Thanks @ Chrisir. I should have been clearer. This is the one dimensional CA so no game of life. I actually figured out how to randomly restart it. It is really just the concept of grabbing a different sublist of an array that is tricky for me. Thanks again.

  • Just come back if you still need help on this

  • edited June 2016

    IMPORTANT ADDENDUM

    Class Arrays' toString() method already does the very same "stringify" for arrays:
    http://docs.Oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-int:A-

    Therefore we don't need to implement that ourselves.
    Only con is that we can't pick which prefix & suffix characters to be used. It's always [ & ]. L-)

    /**
     * StringifyArray (v1.1.1)
     * GoToLoop (2016-Jun-23)
     *
     * forum.Processing.org/two/discussion/17283/
     * access-separate-lists-in-2d-array#Item_13
     */
    
    import java.util.StringJoiner;
    import java.util.Arrays;
    
    static final String DELIM = ", ";
    static final String PREFIX = "{", SUFFIX = "}";
    
    @ SafeVarargs static final String stringifyArray(final int... arr) {
      if (arr == null)  return "";
      final StringJoiner sj = new StringJoiner(DELIM, PREFIX, SUFFIX);
      for (final String elem : str(arr))  sj.add(elem);
      return sj.toString();
    }
    
    final int[][] brobes = { {0, 1, 0}, {4, 4, 4}, {4, 5, 5}, {6, 7, 8} };
    
    void setup() {
      for (final int[] subArr : brobes)  println(stringifyArray(subArr));
      println();
      for (final int[] subArr : brobes)  println(Arrays.toString(subArr));
      exit();
    }
    
  • @ Chrisir - I appreciate the help. @GoToLoop - this 'toString()' method is fantastic! Nice technique. Thank you to you both.

Sign In or Register to comment.