英文:
Creating a specific pattern for list of integers
问题
Sure, here's the translated code you requested:
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;
}
}
Please let me know if you need any further assistance!
英文:
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?
答案1
得分: 2
我认为问题在于第4行。代码将一个位置设置为一个值,但这个位置从0重复到7。
这样做应该会更好:
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;
}
}
基本上,它将0到7每个新的数字向右移动了8个位置。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论