我有一个单独的链接列表叫做ranklist。在这个列表中,我插入了诸如id、Map点和不同商店的得分等信息。
我的实现代码:
class RankList {
private Node first;
private int nodeCount;
private Record record;
public static void main(String[] args) {
//Inserting values for a node
//Data for the first node
RankList list = new RankList();
Point point = new Point(5.4, 3.2);
Record record = new Record(1, point, 8.2);
System.out.println(list.insert(record));
double maxDist=point.dist(point);
//Data for the second node
Point point1 = new Point(1.4, 9.2);
Record record1 = new Record(2, point1, 7.5);
if((point1.dist(point)>maxDist)) maxDist=point1.dist(point);
System.out.println(list.insert(record1));
//Data for the third node
Point point2 = new Point(2.2, 1.2);
Record record2 = new Record(3, point2, 6.0);
if((point2.dist(point1)>maxDist)) maxDist=point2.dist(point1);
System.out.println(list.insert(record2));
list.printList(); //Prints the list
}
我得到的清单:
HEAD -> Rank[Identity Number: 1, Location at point: 5.4,3.2, Score: 8.2] -> Rank[Identity Number: 3, Location at point: 2.2,1.2, Score: 6.0] -> Rank[Identity Number: 2, Location at point: 1.4,9.2, Score: 7.5] -> null
问题来了。我需要用以下参数实现一个名为nearest的方法。
public RankList nearest (Point p,double maxDist)
注意:最近的方法在类ranklist范围内。
在这种方法中,我需要将我已经插入到ranklist的所有点与点p进行比较。
例如 Point p = ( 2.2 , 4.4)
带着所有的 Locations at point
我很难理解如何只访问ranklist中节点的point对象,以便使用它进行比较。
我试图实现这个,但它根本不起作用,因为点超出了范围。
public RankList nearest (Point p,double maxDist){
RankList nearList = new RankList();
Node current = first;
while (current != null) {
System.out.print(current);
if(point.dist(p)<maxDist){ //Finding the distance between the added points with p argument
nearList.insert(record); //Insert the distances shorter than maxDist in a new list.
}
current = current.getNext();
}
System.out.println("null");
return nearList;
}
点类:
class Point {
public double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
//Distance between two points
public double dist(Point p) {
return Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
}
public Point copy() {
return new Point(this.x, this.y);
}
@Override
public String toString() {
return "point: "+x+","+y;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!