此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
对整个列表进行处理
- 现在你会把数据存进
ArrayList了。接下来我们处理(process)它。 - 常见的工作:统计(count)符合规则的元素、把它们筛选(filter)到一个新列表里、以及删除(remove)其中一些。
- 这些都用一个遍历列表的循环来完成。
按规则统计
- 走遍列表,测试每个元素,当测试为真时给计数器加
1。 - 当
n为偶数时,n % 2 == 0为真。当n为奇数时,n % 2 != 0为真。 - 这只是读取列表,所以用增强 for 循环就可以。
English
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(4);
nums.add(7);
nums.add(10);
nums.add(3);
int evens = 0;
for (int x : nums) {
if (x % 2 == 0) {
evens = evens + 1;
}
}
System.out.println(evens); // 2
}
}
筛选到一个新列表
- 筛选(filter)只保留通过测试的元素。
- 一个安全的写法:新建一个空的列表,然后把每个通过的元素
add进去。 - 旧列表不会被改变。这样就能避免我们接下来要看的那个 bug。
English
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(4);
nums.add(7);
nums.add(10);
nums.add(3);
ArrayList<Integer> bigOnes = new ArrayList<Integer>();
for (int x : nums) {
if (x >= 5) {
bigOnes.add(x);
}
}
System.out.println(bigOnes); // [7, 10]
}
}
边遍历边删除的 bug
- 它看起来很简单:向前循环,把不要的元素
remove(i)掉。 - 但
remove(i)会将后续所有元素左移一位。循环随后将1加到i上,并跳过移入原位置的元素。 - 下面的例子想删除所有偶数,却漏掉了一个。
English
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(4); // this one gets skipped!
nums.add(5);
// BUGGY: forward loop while removing
for (int i = 0; i < nums.size(); i++) {
if (nums.get(i) % 2 == 0) {
nums.remove(i);
}
}
System.out.println(nums); // [4, 5] -- wrong, 4 was missed
}
}
为什么会跳过
- 开始:
[2, 4, 5],i = 0。2是偶数,删除它。列表变成[4, 5]。 4下移至索引0。但循环现在设置i = 1。- 在
i = 1时我们看的是5,而不是4。4从来没有被检查过,于是留了下来。
修复办法:倒着循环
- 从最后一个下标一直往下走到
0。 - 当你删除下标
i时,只有i后面的元素会移动 —— 而那些你已经走过了。 - 你还要访问的下标都比
i更小,所以不会有元素从你脚下移走。
English
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(4);
nums.add(5);
// SAFE: backward loop while removing
for (int i = nums.size() - 1; i >= 0; i--) {
if (nums.get(i) % 2 == 0) {
nums.remove(i);
}
}
System.out.println(nums); // [5] -- correct
}
}
关于增强 for 循环的提醒
- 你可能会想在
for (int x : nums)里面删除元素。 - 不要这样做。 在增强型 for 循环中更改列表大小通常会抛出
ConcurrentModificationException,即使未抛出,元素也会被静默跳过。应使用索引循环代替。 - 要删除元素,永远使用上面那种倒着的下标循环。
常见错误
- 用下标正向遍历时删除会漏掉下一个(remove bug)。
- 删除时倒着遍历,或用迭代器。
现在轮到你
- 每个任务都已经填好了类的骨架 —— 把你的代码写在 main 里面,或者补全给出的方法。
- 使用 Check answer(检查答案)将您的代码与提供的测试一起编译并查看结果。请保留启动文件中显示的类和接口。
- 你的代码在服务器上编译并运行,所以第一次运行也很快。
探索
Filtering a list
Removing while looping shifts indices — step through to see why.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Complete countOdds(ArrayList<Integer> a) so it returns how many numbers in the list are odd. A number is odd when x % 2 != 0.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Complete keepPositives(ArrayList<Integer> a). Build and return a 新的 ArrayList<Integer> that holds only the numbers greater than 0, in their original order. Do 不 变化 a.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Complete removeEvens(ArrayList<Integer> a) so it removes every even number from a itself. Loop backwards by index so you do not skip items. Return nothing (void).
点击“运行”查看此处输出。