一个关于Java链表循环遍历,排序的问题。高手上了!!!
发布网友
发布时间:2022-05-09 19:29
我来回答
共1个回答
热心网友
时间:2023-10-15 05:20
package com.neusoft.main;
import java.util.ArrayList;import java.util.List;
public class TestCoordinate {
/**
* <p>Discription:[方法功能中文描述]</p>
* @param args
* @author:[滕一汉]
* @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
*/
public static void main(String[] args) {
List list = new ArrayList();
list.add( new CoordinateObj(10,10));
list.add( new CoordinateObj(20,20));
list.add( new CoordinateObj(30,30));
list.add( new CoordinateObj(40,40));
list.add( new CoordinateObj(50,50));
list.add( new CoordinateObj(60,60));
list.add( new CoordinateObj(70,70));
list.add( new CoordinateObj(80,80));
list.add( new CoordinateObj(90,90));
list.add( new CoordinateObj(100,100));
getNearestCoor(list, new CoordinateObj(0, 0));
}
public static CoordinateObj getNearestCoor(List list, CoordinateObj nowCoor) {
double dis = 0d;
CoordinateObj newcoo = null;
for (int i = 0; i < list.size(); i++) {
CoordinateObj coo = (CoordinateObj) list.get(i);
double x = coo.getX() - nowCoor.getX();
double y = coo.getY() - nowCoor.getY();
double newDis = Math.hypot(x, y);
if (dis == 0) {
dis = newDis;
newcoo = coo;
}
else {
if (newDis < dis) {
newcoo = coo;
dis = newDis;
}
}
}
list.remove(newcoo);
System.out.println("x:" + newcoo.getX() + "---y:" + newcoo.getY());
if(list.size()>0)
getNearestCoor(list,newcoo);
return newcoo;
}
}
package com.neusoft.main;
public class CoordinateObj {
public CoordinateObj(){}
CoordinateObj(double x,double y){
this.x = x;
this.y = y;
}
double x;//x 坐标
double y;//y 坐标
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}