问题是,"我们可能期望method3比method2运行得更快,为什么会这样?" 但我不知道.似乎两种方法都执行相同数量的操作.有人可以赐教吗?
ArrayList<Person> method2(Person x, ArrayList<Person> people){
ArrayList<Person> friends = new ArrayList<Person>();
for (Person y : people) if (x.knows(y)) friends.add(y);
return friends;
}
ArrayList<Person> method3(Person x, ArrayList<Person> people){
ArrayList<Person> friends = new ArrayList<Person>();
for (int=0; i<people.size(); i++){
Person y = people.get(i);
if (x.knows(y)) friends.add(y);
}
return friends;
}
Run Code Online (Sandbox Code Playgroud)