开发者

Java Arraylist Help

Alright so I'm trying to get this class work:

public开发者_如何学运维 boolean hasPoint(Point p){

    for (int i=0; i<this.points.size(); i++){
        // Right here
        if(points[i].equals(p)){
            return true; 
        }

    }
    return false;     
}

However on line 3 I seem to be calling points as an array, but it's actually an arraylist. What am I doing wrong?


To access elements of an ArrayList, use .get():

public boolean hasPoint(Point p){

    for (int i=0; i<this.points.size(); i++){
        if (points.get(i).equals(p)){
            return true; 
        }
    }

    return false;     
}

But if points is an ArrayList, you could just use ArrayList.contains() to the same effect:

public boolean hasPoint(Point p) {
    return points.contains(p);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜