Creating a specific pattern for list of integers

newuser :
int[] box = new int[9*8];
for(int i=0; i<9; i++) {
    for(int j=0; j<8; j++) {
        box[j] = i;
    }
}

I've tried everything and it turns out to be way harder than it looks for me. Without using ArrayLists (I understand this works using box.add(i)) I can only use int[] type. I need to create a list of integers that looks like this [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,..8,8,8,8,8,8,8,8] so 8 sets of integers from 0-8. Can anyone help me?

mega12345mega :

I believe the problem is that on line 4. The code sets a position on to a value, but this position repeats from 0 to 7.

This should work better:

int[] box = new int[9*8];
for(int i = 0; i < 9; i++) {
    for(int j = 0; j < 8; j++) {
        box[i * 8 + j] = i;
    }
}

Basically, it shift the 0 - 7 over 8 places for every new number.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403708&siteId=1