How do i split an arraylist of string into multiples of 8?

edited February 2015 in How To...

I have an arraylist of strings that contains values of 0 and 1. I want to split the arraylist to multiples of 8 and store them seperately in another arraylist. For example, i have a arraylist of 0 and 1s. I take the first 8 digits then store in array, then the next 8 store into second index of the second array. If that makes any sense.

I have seen the split() function but I haven't figured out using on an arraylist.

Answers

  • You can't.

    Just use a for loop and split it however you want.

  • its more clear if i rephrase my question as. How do i split an arraylist at every nth position and store in an array?

    Example

    Input - ArrayList = [0,1,1,0,1,0,1,0,1,0,1,0,0,0,1,1,0]

    if n = 8, i take every nth position and store into array so

    Output - Array =[011010101, 01000110]

  • //Array of ints
    int[] in = {0,1,1,0,0,0,1,1,
                1,1,1,1,1,1,0,0,
                1,0,1,0,1,0,1,0};
    String[] out = new String[in.length/8];
    int j = -1;
    for(int i = 0; i < out.length; i++) out[i] = "";
    for(int i = 0; i < in.length; i++){
      if(i%8==0) j++;
      //println("i="+i+" j="+j);
      out[j] = out[j] + char('0'+in[i]);
      //println("out["+j+"]="+out[j]);
    }
    for(int i = 0; i < out.length; i++) println( out[i] );
    
  • thanks TfGuy44 for the reply but can i ask what does char('0'+in[i]); means?

  • println( "1) " + char(0) );
    println( "2) " + char('0') );
    println( "3) " + int('0') );
    println( "4) " + char(48) );
    println( "5) " + char(49) );
    println( "6) " + char('0'+0) );
    println( "7) " + char('0'+1) );
    
  • edited March 2015

    My more complicated solution too. A little l8 for the party it seems: :O)

    /**
     * UnifyStringListAsArray (v1.02)
     * by  GoToLoop (2015/Feb/24)
     * for Timhua
     * 
     * forum.processing.org/two/discussion/9566/
     * how-do-i-split-an-arraylist-of-string-into-multiples-of-8
     */
    
    static final String[] unifyStringListAsStringArray(StringList list, int n) {
      if (list == null || list.size() == 0 || n < 1)  return new String[0];
    
      int len = list.size(), seqs = 1 + (len-1)/n; // js: seqs = 1 + (0 | (len-1)/n);
      String[] joined  = new String[seqs];
      StringBuilder sb = new StringBuilder(n);
    
      for (int i = 0; i < len; i += n) {
        for (int j = i, qty = min(j + n, len); j != qty; sb.append(list.get(j++)));
        joined[i/n] = sb.toString(); // js: joined[i/n | 0] = sb.toString();
        sb.delete(0, sb.length());
      }
    
      return joined;
    }
    
    static final int SPLIT = 8;
    
    final StringList myList = new StringList(new String[] {
      "0", "1", "1", "0", "1", "0", "1", "0",
      "1", "0", "1", "0", "0", "0", "1", "1",
      "0", "1", "0"
    });
    
    String[] result;
    
    void setup() {
      println(myList, '\n');
      result = unifyStringListAsStringArray(myList, SPLIT);
      println(result);
      exit();
    }
    
  • GoToLoops it does seem more complicated :)) i'll try to slowly dissect your code thanks for reply

  • I get an error called "The method parseChar(byte) is the type PApplet is not application for the arguments (String)"

    What does this mean?

  • edited February 2015
    • AFAIK, my code sample works in any Processing 2.1.x family and on!
    • I'm currently using 64-bit Processing version 2.2.1 now @ Lubuntu Linux.
    • Error message means that datatype byte isn't acceptable for PApplet's parseChar() method.
  • edited March 2015 Answer ✓
    • I've noticed that in @TfGuy44's code an int[] is used as source.
    • I remember you specified yours was an ArrayList of String objects.
    • That's why in my own I've used a similar structure called StringList instead:
      http://processing.org/reference/StringList.html
    • Now I've modified unifyStringListAsStringArray() to accept a byte[] now.
    • If you prefer int[] or char[], etc., just a matter of replacing those byte[] declarations w/ it.
    • It's renamed to unifyByteArrayAsStringArray(). Also changed for () to while (). Check it out!
    • Any doubts about it just ask! :>

    /**
     * unifyByteArrayAsStringArray (v1.11)
     * by  GoToLoop (2015/Feb/25)
     * for Timhua
     * 
     * forum.processing.org/two/discussion/9566/
     * how-do-i-split-an-arraylist-of-string-into-multiples-of-8
     */
    
    static final String[] unifyByteArrayAsStringArray(byte[] arr, int n) {
      if (arr == null || arr.length == 0 || n < 1)  return new String[0];
    
      int i = 0, len = arr.length, seqs = 1 + (len-1)/n; // js: seqs = 1 + (0 | (len-1)/n);
      String[] joined  = new String[seqs];
      StringBuilder sb = new StringBuilder(n);
    
      while (i != len) {
        int qty = min(i + n, len);
        while (i != qty)  sb.append(arr[i++]);
        joined[(i-1)/n] = sb.toString(); // js: joined[i/n | 0] = sb.toString();
        sb.delete(0, sb.length());
      }
    
      return joined;
    }
    
    static final int SPLIT = 8;
    
    final byte[] myArray = {
      0, 1, 1, 0, 1, 0, 1, 0,
      1, 0, 1, 0, 0, 0, 1, 1,
      0, 1, 0
    };
    
    String[] result;
    
    void setup() {
      println(myArray);
      println();
      result = unifyByteArrayAsStringArray(myArray, SPLIT);
      println(result);
      exit();
    }
    
  • GoToLoops I dont quite understand this piece of code that you coded can you explain it to me?

    
      int i = 0, len = arr.length, seqs = 1 + (len-1)/n;
      String[] joined  = new String[seqs];
      StringBuilder sb = new StringBuilder(n);
     
      while (i != len) {
        int qty = min(i + n, len);
        while (i != qty)  sb.append(arr[i++]);
        joined[(i-1)/n] = sb.toString();
        sb.delete(0, sb.length());
      }
     
      return joined;
    
    
  • edited March 2015

    Actually I dunno whether your doubts are related to StringBuilder class or the algorithm itself! :-/
    You can read about StringBuilder class below:
    https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

    StringBuilder was chosen for performance reasons.
    Regular String w/ the + concatenator operator yield the same results:
    http://processing.org/reference/addition.html

    Simpler String w/ += concatenation version:

    static final String[] unifyByteArrayAsStringArray(byte[] arr, int n) {
      if (arr == null || arr.length == 0 || n < 1)  return new String[0];
    
      int i = 0, len = arr.length, seqs = 1 + (len-1)/n;
      String[] joined = new String[seqs];
      String s = "";
    
      while (i != len) {
        int qty = min(i + n, len);
        while (i != qty)  s += arr[i++];
        joined[(i-1)/n] = s;
        s = "";
      }
    
      return joined;
    }
    

    Compare the above w/ the original StringBuilder version here:

    static final String[] unifyByteArrayAsStringArray(byte[] arr, int n) {
      if (arr == null || arr.length == 0 || n < 1)  return new String[0];
    
      int i = 0, len = arr.length, seqs = 1 + (len-1)/n;
      String[] joined  = new String[seqs];
      StringBuilder sb = new StringBuilder(n);
    
      while (i != len) {
        int qty = min(i + n, len);
        while (i != qty)  sb.append(arr[i++]);
        joined[(i-1)/n] = sb.toString();
        sb.delete(0, sb.length());
      }
    
      return joined;
    }
    

    If it's something else, please point where it is so I can explain it! :-bd

Sign In or Register to comment.