printing 2darray as text...

Hi, i've searched for a while and I'm not sure if i'm missing something obvious, i'm trying to print a 2d integer array as text, i did it before but I can't remember how i did and it is driving me insane.

thanks.

void setup() {

  size(300, 300);
  background(255);
  array();
}

void array() {

  int[][] arrayinit = {
    { 10, 5, 3, 55, 90 }, 
    {  9, 4, 2, 32, 22 }, 
    {  3, 8, 5, 79, 22 }, 
    {  3, 8, 45, 4, 2 }, 
    {  3, 22, 5, 44, 4 }
  };

  int rows = 5;
  int cols = 5;

  int[] sum = new int[cols];
  int[] sum2 = new int[rows];
  stroke(0);
  for (int c=0; c<cols; c++) { // for every column
    for (int r=0; r<rows; r++) { // for every row
      rect(40*r, 30*c, 40, 30);
      text(arrayinit[c][r], 40*r, 30*c); //dont know if this is right
      sum[c] += arrayinit[r][c]; // sum up the values per column
      sum2[r] += arrayinit[r][c];
    }
  }

  println(sum[0]); 
  println(sum[1]); 
  println(sum[2]); 
  println(sum2[2]);
  println(arrayinit[2][2]);
}

Answers

  • edited November 2017

    From https://Forum.Processing.org/two/discussion/23471/combining-two-pieces-of-code/p2#Item_51

    I've made this utility function to join a 2D String[][] array as 1 String, ready for text(): :ar!

    static final String joinStrArr2d(final String[][] arr2d) {
      final StringBuilder sb = new StringBuilder();
      int outer = 0;
    
      for (final String[] arr1d : arr2d) {
        sb.append(ENTER);
        int inner = 0;
    
        for (final String item : arr1d)  sb
          .append('[').append(outer).append("][").append(inner++)
          .append("] ").append(item).append(ENTER);
    
        ++outer;
      }
    
      return sb.toString();
    }
    
  • You are drawing both the rectangle and text. But both has white color as their fill. Even the background. So it's pretty much invisible. You could simply change your text color to solve the problem.

    It will look like this:

      for (int c=0; c<cols; c++) { // for every column
        for (int r=0; r<rows; r++) { // for every row
          fill(255);
          if(c < rows-1)rect(40*r, 30*c, 40, 30);
          fill(0);
          text(arrayinit[c][r], 40*r, 30*c); //dont know if this is right
          sum[c] += arrayinit[r][c]; // sum up the values per column
          sum2[r] += arrayinit[r][c];
        }
      }
    

    Best regards :D

  • Answer ✓
    int[][] arrayinit = {
      { 10, 5, 3, 55, 90 }, 
      {  9, 4, 2, 32, 22 }, 
      {  3, 8, 5, 79, 22 }, 
      {  3, 8, 45, 4, 2 }, 
      {  3, 22, 5, 44, 4 }
    };
    
    void setup() {
      size(300, 300);
      background(255);
      textAlign(CENTER);
    }
    
    void draw() {
    
      int rows = 5;
      int cols = 5;
    
      stroke(0);
    
      for (int c=0; c<cols; c++) { // for every column
        for (int r=0; r<rows; r++) { // for every row
    
          // draw rect
          fill(255); // white 
          rect(40*r+43, 30*c+43, 
            40, 30);
    
          // draw text into rect
          fill(255, 0, 0); // red 
          text(arrayinit[c][r], 
            40*r+43+20, 30*c+43+20);
        }
      }
    }
    
  • Thanks, as I thought it was pretty obvious.

  • Another alternate approach is to use a fixed-width font and print a single line-wrapped string. This will create tabular text on the screen quickly and easily using the built-in text system. However, the method is inflexible (i.e. space padding is required), and it is very sensitive to character width and line height.

  • Belatedly: Another option is a simple trick that uses Java Arrays.deepToString() and then reformats the output with simple search-replace rules.

    import java.util.Arrays;
    int[][] arrayinit = {
      {  1, 5, 3 }, 
      {  9, 4, 2 }, 
      {  3, 8, 6 }, 
    };
    void setup() {
      println(Arrays.deepToString(arrayinit);
      println();
      println(Arrays.deepToString(arrayinit)
        .replace("[[", "")
        .replace("], [", "\n")
        .replace("]]", "")
        .replace(" ", "  "));
    }
    

    Output:

    [[1, 5, 3], [9, 4, 2], [3, 8, 6]] 
    
    1,  5,  3
    9,  4,  2
    3,  8,  6
    

    Related:

Sign In or Register to comment.