【发布时间】:2018-05-05 00:58:42
【问题描述】:
我有一个静态方法,我需要完成它。
我要做的是使用迭代器来检查 y 坐标是否大于最大值并返回该值。这就是我现在所拥有的。
import java.awt.*;
public static Point highestPoint(List<Point> points) {
Iterator<Point> pointIterator = points.iterator();
int highest = 0;
Point highestPoint = null;
while (pointIterator.hasNext()) {
pointIterator.next()
if (points.getY() > highest) {
highest = points.get(Y);
}
}
return highestPoint;
}
当我运行我的代码时,我收到一个语法错误:
Main.java:12: error: cannot find symbol
if (points.getY() > highest) {
^
symbol: method getY()
location: variable points of type List<Point>
Main.java:13: error: cannot find symbol
highest = points.getY();
^
symbol: method getY()
location: variable points of type List<Point>
2 errors
【问题讨论】:
-
我会使用
points.stream().mapToInt(Point::getY).max().orElse(IllegalArgumentException::new) -
还没有人反对这个? 1. 您不能在列表 2 上使用 getY()。为什么需要使用迭代器,任何特定的用例?使用流最大功能
-
@Deepak If you think thats bad..。所以偏离了它的高质量问答库的标准。它已经转向“解决他们的问题。他们不再有这个问题,你就会获得代表。代表会吸引你的个人资料。”我曾经是about quality (example )。现在看看我。
-
@Vince,一开始我只是在讽刺和搞笑。无意冒犯,但我问的是一个简单的问题,为什么需要迭代器。如果除了查找 max 之外不需要创建额外的 interator 对象,则使用 stream
-
你显然不知道downvote按钮的用途,@Deepak