创建一个特定的整数列表模式

huangapple go评论63阅读模式
英文:

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&lt;9; i++) {
	for(int j=0; j&lt;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 &lt; 9; i++) {
    for(int j = 0; j &lt; 8; j++) {
        box[i * 8 + j] = i;
    }
}

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

huangapple
  • 本文由 发表于 2020年4月4日 11:39:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61023469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定