简体   繁体   中英

Creating a specific pattern for list of integers

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM