我正在扫描一个CSV文件,看看哪些值适合在给定的框内。CSV文件具有X和Y坐标,函数的参数是框的周长(即顶部、底部、左侧、右侧)。我不想使用任何外部库;其部分目的是学习如何手动加速代码。
我对Java相当陌生,所以任何输入都是有帮助的。如果可能的话,我试着把这个降低到100毫秒,但是现在它大约需要1000毫秒。
Scanner dataStream = new Scanner(fileMain);
while (dataStream.hasNext()){
String data = dataStream.next();
String[] values = data.split(",", 3);
if(Double.parseDouble(values[0]) > lowX && Double.parseDouble(values[0]) < highX){
if(Double.parseDouble(values[1]) < highY && Double.parseDouble(values[1]) > lowY){
System.out.println(data);
}
dataStream.close();数据的格式如下:
90.92, 102.3
40.28, 82.32
13.02, 80
72, 104.82发布于 2016-05-16 07:55:35
首先,请注意,您始终可以使用Point2D.Double来表示平面上的点。
接下来,我将您的程序分成两种方法:第一种用于修剪不在指定边界框内的点,第二种用于从标准输入中读取这些点,并将它们放入第一种。
对于编码约定,在if语句之前我将有一个空行。另外,您应该用一个空格包围if语句的条件。所以,而不是
String[] values = data.split(",", 3);
if(Double.parseDouble(values[0])){ ...你应该写
String[] values = data.split(",", 3);
if (Double.parseDouble(values[0])) { ...总之,我有这样的想法:
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public class CoordinateUtilities {
public static List<Point2D.Double>
getPointsWithinBoundingBox(final List<Point2D.Double> pointList,
final double lowX,
final double highX,
final double lowY,
final double highY) {
Objects.requireNonNull(pointList, "The list of points is null.");
checkXBounds(lowX, highX);
checkYBounds(lowY, highY);
final List<Point2D.Double> ret = new ArrayList<>(pointList.size());
for (final Point2D.Double point : pointList) {
final double x = point.x;
final double y = point.y;
if (lowX <= x && x <= highX && lowY <= y && y <= highY) {
ret.add(point);
}
}
return ret;
}
public static void main(final String... args) {
final Scanner scanner = new Scanner(System.in);
final double lowX = 50.0;
final double highX = 100.0;
final double lowY = 30.0;
final double highY = 60.0;
final List<Point2D.Double> allPoints = new ArrayList<>();
while (scanner.hasNextLine()) {
final String line = scanner.nextLine();
if (line.trim().equals("quit")) {
break;
}
final Point2D.Double point = parsePoint(line);
if (point != null) {
allPoints.add(point);
}
}
final List<Point2D.Double> boundedPoints =
getPointsWithinBoundingBox(allPoints, lowX, highX, lowY, highY);
boundedPoints.forEach(System.out::println);
}
private static Point2D.Double parsePoint(final String line) {
final String[] tokens = line.trim().split("\\s*,\\s*");
if (tokens.length < 2) {
return null;
}
try {
final double x = Double.parseDouble(tokens[0]);
final double y = Double.parseDouble(tokens[1]);
return new Point2D.Double(x, y);
} catch (final NumberFormatException ex) {
return null;
}
}
private static void checkXBounds(final double lowX, final double highX) {
if (Double.isNaN(lowX)) {
throw new IllegalArgumentException(
"The lower bound of the x-coordinate is NaN.");
}
if (Double.isNaN(highX)) {
throw new IllegalArgumentException(
"The upper bound of the x-coordinate is NaN.");
}
if (lowX > highX) {
throw new IllegalArgumentException(
"The lower bound of the x-coordinate (" + lowX + "is " +
"larger than the upper bound of the x-coordinate (" +
highX + ").");
}
}
private static void checkYBounds(final double lowY, final double highY) {
if (Double.isNaN(lowY)) {
throw new IllegalArgumentException(
"The lower bound of the y-coordinate is NaN.");
}
if (Double.isNaN(highY)) {
throw new IllegalArgumentException(
"The upper bound of the y-coordinate is NaN.");
}
if (lowY > highY) {
throw new IllegalArgumentException(
"The lower bound of the y-coordinate (" + lowY + "is " +
"larger than the upper bound of the y-coordinate (" +
highY + ").");
}
}
}希望这能帮上忙。
https://codereview.stackexchange.com/questions/128450
复制相似问题