将迭代中的当前元素与数组中的所有后续元素进行比较 - for 循环
Compare the current element in iteration with all the following elements in an array - for loop
int[] num = new int[] { 20, 19, 8, 9, 12 };
例如上面的数组,我需要比较:
- 元素 20 与元素 19、8、9、12
- 元素 19 与 8、9、12
- 元素 8 与 9、12
- 元素 9 和 12
两个循环:
// Go through each element in turn (except the last one)
for (int i = 0; i < num.Length - 1; i++)
{
// Start at the element after the one we're on, and keep going to the
// end of the array
for (int j = i + 1; j < num.Length; j++)
{
// num[i] is the current number you are on.
// num[j] is the following number you are comparing it to
// Compare num[i] and num[j] as you want
}
}
int[] num = new int[] { 20, 19, 8, 9, 12 };
例如上面的数组,我需要比较:
- 元素 20 与元素 19、8、9、12
- 元素 19 与 8、9、12
- 元素 8 与 9、12
- 元素 9 和 12
两个循环:
// Go through each element in turn (except the last one)
for (int i = 0; i < num.Length - 1; i++)
{
// Start at the element after the one we're on, and keep going to the
// end of the array
for (int j = i + 1; j < num.Length; j++)
{
// num[i] is the current number you are on.
// num[j] is the following number you are comparing it to
// Compare num[i] and num[j] as you want
}
}