views:

185

answers:

4

I have an array, say

int a[]={2,0,1,0,1,1,0,2,1,1,1,0,1,0,1};

I need to append each of the 5 neighboring elements and assign them to a new array b with length=(a.length/5); and i want to append the 5 neighboring elements so that I have: int b[]={20101,10211,10101}; I need to do this for various length arrays, in most cases with length of a being greater than 15.

Any help would be greatly appreciated, I'm programming in Java.

Thanks in advance.

+2  A: 

It's pretty straighforward:

// Assuming a.length % 5 == 0.
int[] b = new int[a.length / 5];
for (int i = 0; i < a.length; i += 5) {
    b[i/5] = a[i]*10000 + a[i+1]*1000 + a[i+2]*100 + a[i+3]*10 + a[i+4];
}
Marcelo Cantos
If a.length does not divide evenly by 5 the array will not be big enough, and various exceptions will be thrown inside the for loop.
Finbarr
Thansk, that works fine, but when I'm dealing with integers starting with 0, with the above method I lose the 0, for example:a={0,2,2,2,2,1,0,2,0,2}; I get b={2222,10202}; instead of b={02222,10202}; which is what i need. Any way to stop that happening?
Neville
@Finbarr, the length of a will always be divisible by 5, so thankfully that won't be a problem.
Neville
If you want to keep the leading zeroes, you cannot store these as integers. Use Strings instead.
Syntactic
If you need to keep the leading zeroes, then b cannot be an `int[]`, because numbers can't have leading zeroes. You could make it for example a `String[]`.
Jesper
thanks, yea I'll use a String to do that, thanks everyone.
Neville
+1  A: 
Michael Aaron Safyan
+1 for not giving the solution
klez
+1  A: 

This works with (a.length % 5) != 0, and keeps leading zeroes (by storing digits into String).

    int a[]={2,0,1,0,1,1,0,2,1,1,1,0,1,0,1,0,0,7};

    final int N = 5;
    String b[] = new String[(a.length + N - 1)/ N];
    StringBuilder sb = new StringBuilder(N);
    int x = 0;
    for (int i = 0; i < b.length; i++) {
        sb.setLength(0);
        for (int k = 0; k < N && x < a.length; k++) {
            sb.append(a[x++]);
        }
        b[i] = sb.toString();
    }
    System.out.println(java.util.Arrays.toString(b));
    // prints "[20101, 10211, 10101, 007]"

Alternately, you can also use regex:

    String[] arr =
        java.util.Arrays.toString(a)
        .replaceAll("\\D", "")
        .split("(?<=\\G.{5})");
    System.out.println(java.util.Arrays.toString(arr));
    // prints "[20101, 10211, 10101, 007]"

Basically this uses Arrays.toString(int[]) to append all digits into one long String, then removes all non-digits \D, then uses \G-anchored lookbehind to split every .{5}

polygenelubricants
A: 

Naive approach.

import java.util.ArrayList;

/* Naive approach */
public class j2728476 {
    public static void main(String[] args) {
        int a[] = {2,0,1,0,1,1,0,2,1,1,1,0,1,0,1};
        ArrayList<String> al = new ArrayList<String>();
        String s = "";
        for (int i = 0; i < a.length; i++) {
            if (i % 5 == 0 && i != 0) {
                al.add(s);
                s = "" + a[i];
            } else {
                s += a[i];
            }
        }
        al.add(s);
        for (String t : al) {
            // convert values to ints ...
            System.out.println(t);
        }
    }
}

Will print:

20101
10211
10101
The MYYN