How to join() using more dimensional arrays?

I've made some pretty nice 2d arrays, but I found some problems with it while trying to save the number pattern to text. I either got nothing or an error, or a messy text file with numbers and nulls mixed up in it. I think the most problematic thing is the join() command, and the fact that I can't tell it which array slot i'm interested in while joining them. All in all, I'm kinda stuck and this path might not be the right one, but here it is what I was trying to do (help is very appreciated):

    short i=1;short j=0;

    for(i=1 ;i<24; i++) {
      for(j=0 ;j<35; j++) {
        pieceData[i][j] = str(patternXY[i][j]);
      }
      mainData[i] = join(pieceData[i],",");
    }

    saveStrings("data/text"+001+".txt" , mainData);
Tagged:

Answers

  • edited May 2018
    static final String[] join2D(final String[][] arr2d, final char sep) {
      return join2D(arr2d, str(sep));
    }
    
    static final String[] join2D(final String[][] arr2d, final String sep) {
      final int len = arr2d.length;
      final String[] joined = new String[len];
      for (int i = 0; i < len; joined[i] = join(arr2d[i++], sep));
      return joined;
    }
    
  • Not tested. This is just an idea.

    Kf

    final int SEP=',';  //Comma separator
    final int N=24;
    final int M=35;
    String[] mainData = new String[N];
    
    short i=1;short j=0;
    
    for(i=1 ;i<N; i++) {;
    
      String rowStr="";  // EXERCISE: Use StringBuilder() instead
    
      for(j=0 ;j<M; j++) {
        if(j!=0) rowStr +=SEP
        rowStr += str(patternXY[i][j]);
      }
    
      mainData[i] = rowStr;
    }
    
    saveStrings("data/text"+001+".txt" , mainData);
    
Sign In or Register to comment.