java - 如何比较列表和方法中的值?
问题描述
我有一个包含不同类型数据的 LinkedList,我需要处理这些数据以进行比较并添加与范围一致的值。下面将给出更多解释,
LinkedList
填充了Record 类的数据:
class Record {
public int id;
public Point location;
public double score;
(...)
}
点类:
class Point {
public double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double dist(Point p) {
return Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
}
排名列表类:
class RankList {
private Node first;
private int nodeCount;
private Record record;
public static void main(String[] args) {
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);
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));
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.nearest(point1,maxDist);
我在列表中插入了一些值,假设我在给定点之间有一些距离值,例如:
Distance between two points:
A->B = 3.2455
B->C = 7.345
C->D = 2.111
D->E = 8.056
从此maxDist
值为 8.059
现在我必须编写一个方法来查找范围(<=maxDist)public RankList nearest (Point p,double maxDist)
之间的所有距离值,并将它们与其余节点值一起返回到一个列表中。所以我需要用 LinkedList 的指针和给定的参数计算距离,并将它们添加到一个新列表中。Point p
我的问题是我是否可以访问LinkedList
已经用值实现的内容并将我需要的内容复制到新的列表结构中。
最近的方法:
public RankList nearest (Point p,double maxDist){
RankList nearList = new RankList();
Node current = first;
System.out.print("HEAD -> ");
while (current != null) {
System.out.print(current);
System.out.print(" -> ");
current = current.getNext();
}
System.out.println("null");
return null;
}
我尝试LinkedList
使用传统方式运行整个程序,但我堆叠了如何进行比较并将它们添加到新列表中。
有什么建议么?
解决方案
我对你的代码有点困惑。我在里面找不到任何LinkedList
东西。您的班级RankList
似乎只有三个字段,而且都不是 List 类型。我不确定该insert
方法的作用。
使用该main
方法在类中包含字段并不是最好的主意。如果您准备另一个运行该程序的类会更好。
我想展示我如何编写代码:
import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
class Main {
public static void main(String[] args) {
// I don't want the list to be a field of the class.
RecordsList list = new RecordsList();
// Following code blocks could be extracted as separate methods
Point point = new Point(5.4, 3.2);
Record record = new Record(1, point, 8.2);
list.add(record);
double maxDist = point.dist(point);
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);
list.add(record1);
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);
list.add(record2);
Point farPoint = new Point(50, 50);
Record recordWithFarPoint = new Record(4, farPoint, 5);
list.add(recordWithFarPoint);
RecordsList nearestList = list.nearest(point1, 20);
for (Record rec : nearestList.getList()) {
System.out.println(rec.id + " " + rec.location.x + " " + rec.location.y + " " + rec.score);
}
/*
* On console:
* 2 1.4 9.2 7.5
* 1 5.4 3.2 8.2
* 3 2.2 1.2 6.0
*/
}
}
/**
* This class is so-called wrapper on ArrayList.
*/
class RecordsList {
// This may be called delegate.
private ArrayList<Record> list = new ArrayList<>();
public void add(Record record) {
this.list.add(record);
}
// This creates shallow copy.
public ArrayList<Record> getList() {
return new ArrayList<>(list);
}
public RecordsList nearest(Point p, double maxDistance) {
RecordsList list = new RecordsList();
List<Record> records = this.getList().stream()
.sorted(Comparator.comparingDouble(oldListElement -> oldListElement.location.dist(p)))
.filter(element -> element.location.dist(p) <= maxDistance)
.collect(Collectors.toList());
for (Record record : records) {
list.add(record);
}
return list;
}
}
class Record {
public int id;
public Point location;
public double score;
public Record(int id, Point location, double score) {
this.id = id;
this.location = location;
this.score = score;
}
}
class Point {
public double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double dist(Point p) {
return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
}
}
有些概念对您来说可能是新的。最复杂的可能是streamApi。您可以在这里阅读更多相关信息:https ://www.baeldung.com/java-8-streams
如果您有任何问题,或者我误解了您的问题,请随时提问。
推荐阅读
- mysql - Mysql选择查询如果条件参数为空则忽略该参数
- c# - How can I set the position variable only in it's parent rather than on every inherited class?
- android - 是否可以使用opengl在android中压缩视频?
- python - How to show the payment which is assigned to user in html page of django?
- c# - Is it possible to make a var class?
- sql - 在 JOIN(SQL 服务器)之后,有没有更好的方法来针对结果表编写 UPDATE 查询?
- yocto - Is there a way to install keys into the opkg-keyring during the yocto build process and have them marks as ultimate rather than unknown by gpg
- python - PostgreSQL PL/Python 无法加载库 plpython3u
- ios - webRTC blurred screen on iOS 13
- swift - 在 Swift 应用程序中调用时,Bash 没有从“哪个”返回值