数组索引的模运算
Modulus operation for array index
我得到了一个关于使用按钮切换一些图像的教程,这是代码
public class MainActivity extends AppCompatActivity {
private static ImageView andro;
private static Button buttonswitch;
int current_image_index = 0;
int[] images = {R.mipmap.andro_img,R.mipmap.apple_image,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
}
public void buttonClick() {
andro = (ImageView) findViewById(R.id.imageView);
buttonswitch = (Button) findViewById(R.id.button);
buttonswitch.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
current_image_index++;
current_image_index = current_image_index % images.length;
andro.setImageResource(images[current_image_index]);
}
}
);
}
}
这部分我真的很困惑:
@Override
public void onClick(View view) {
current_image_index++;
current_image_index = current_image_index % images.length;
andro.setImageResource(images[current_image_index]);
我的理解是,一旦我点击按钮,int current_image_index 将增加 1。然后模数 current_image_index 与 images.length 将具有 current_image_index 除以 image.length。例如,第一次我会有 current_image_index = 0,然后一旦点击,它将是 1,然后 current_image_index % image.length = 0。然后 andro.setImageResource(images [0]);
由于current_image_index一直为0,所以会反复出现。那么点击图片怎么会不断变化,因为current_image_index%image.length总是给结果为 0.
current_image_index % images.length
作为一个模块工作。
https://en.m.wikipedia.org/wiki/Modulo_operation
所以我认为我们都同意 1/2 = 0 R 1
。
在每种编程语言中取模意味着简单地取除法的余数,return它作为运算的结果。
所以 1 ‰ 2 = 1
而不是零。
...since the current_image_index%image.length will always give a result of 0.
不太正确。
取模运算符 (%
) 计算两个操作数的 remainder。这是一种重复减法。事实上,使用 a % b
你会问自己:
What number remains if I repeat subtracting b
from a
until that operation is no longer possible?
让我们用 8 % 3
来测试它(所以 a = 8
和 b = 3
)。
- 我可以用8减去3吗?是的,结果是 5.
- 我可以用 5 减去 3 吗?是的,结果是 2.
- 我可以用 2 减去 3 吗? 没有,所以我们最后的结果是2.
从逻辑上讲,结果为 r
的操作 a % b
总是导致 0 <= r < b
。
Examples:
5 % 2 = 1 (because 4 ÷ 2 = 2 and the remainder is 1)
17 % 6 = 5 (because 12 ÷ 6 = 2 and the remainder is 5)
20 % 4 = 0 (because 20 ÷ 4 = 5 and nothing remains)
所以在你的例子中,数组索引总是至少 0
和最多 images.length - 1
。这正是您数组的有效范围。
假设您有 3 张图像,因此 images.length
是 3。 current_image_index
也被初始化为 0。所以你会在开头看到 image[0]
。
- 您点击一次,所以
current_image_index
增加到 1
。然后,应用模运算:1 % 3 = 1
.
- 您再次点击,所以
current_image_index
增加到 2
。然后,应用模运算:2 % 3 = 2
.
- 您再次点击,所以
current_image_index
增加到 3
。然后,应用模运算:3 % 3 = 0
。这意味着索引达到 3,但随后立即被取模运算符重置为 0。
所以在image[2]
之后显示image[0]
。您会看到从 0 而不是 1 开始的索引现在对我们有利。
我得到了一个关于使用按钮切换一些图像的教程,这是代码
public class MainActivity extends AppCompatActivity {
private static ImageView andro;
private static Button buttonswitch;
int current_image_index = 0;
int[] images = {R.mipmap.andro_img,R.mipmap.apple_image,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
}
public void buttonClick() {
andro = (ImageView) findViewById(R.id.imageView);
buttonswitch = (Button) findViewById(R.id.button);
buttonswitch.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
current_image_index++;
current_image_index = current_image_index % images.length;
andro.setImageResource(images[current_image_index]);
}
}
);
}
}
这部分我真的很困惑:
@Override
public void onClick(View view) {
current_image_index++;
current_image_index = current_image_index % images.length;
andro.setImageResource(images[current_image_index]);
我的理解是,一旦我点击按钮,int current_image_index 将增加 1。然后模数 current_image_index 与 images.length 将具有 current_image_index 除以 image.length。例如,第一次我会有 current_image_index = 0,然后一旦点击,它将是 1,然后 current_image_index % image.length = 0。然后 andro.setImageResource(images [0]);
由于current_image_index一直为0,所以会反复出现。那么点击图片怎么会不断变化,因为current_image_index%image.length总是给结果为 0.
current_image_index % images.length
作为一个模块工作。
https://en.m.wikipedia.org/wiki/Modulo_operation
所以我认为我们都同意 1/2 = 0 R 1
。
在每种编程语言中取模意味着简单地取除法的余数,return它作为运算的结果。
所以 1 ‰ 2 = 1
而不是零。
...since the current_image_index%image.length will always give a result of 0.
不太正确。
取模运算符 (%
) 计算两个操作数的 remainder。这是一种重复减法。事实上,使用 a % b
你会问自己:
What number remains if I repeat subtracting
b
froma
until that operation is no longer possible?
让我们用 8 % 3
来测试它(所以 a = 8
和 b = 3
)。
- 我可以用8减去3吗?是的,结果是 5.
- 我可以用 5 减去 3 吗?是的,结果是 2.
- 我可以用 2 减去 3 吗? 没有,所以我们最后的结果是2.
从逻辑上讲,结果为 r
的操作 a % b
总是导致 0 <= r < b
。
Examples:
5 % 2 = 1 (because 4 ÷ 2 = 2 and the remainder is 1)
17 % 6 = 5 (because 12 ÷ 6 = 2 and the remainder is 5)
20 % 4 = 0 (because 20 ÷ 4 = 5 and nothing remains)
所以在你的例子中,数组索引总是至少 0
和最多 images.length - 1
。这正是您数组的有效范围。
假设您有 3 张图像,因此 images.length
是 3。 current_image_index
也被初始化为 0。所以你会在开头看到 image[0]
。
- 您点击一次,所以
current_image_index
增加到1
。然后,应用模运算:1 % 3 = 1
. - 您再次点击,所以
current_image_index
增加到2
。然后,应用模运算:2 % 3 = 2
. - 您再次点击,所以
current_image_index
增加到3
。然后,应用模运算:3 % 3 = 0
。这意味着索引达到 3,但随后立即被取模运算符重置为 0。
所以在image[2]
之后显示image[0]
。您会看到从 0 而不是 1 开始的索引现在对我们有利。