基于ArrayList的X、Y坐标距离计算实现求助
基于ArrayList的X、Y坐标距离计算功能实现方案
看起来你在实现坐标存储和距离计算时遇到了瓶颈,核心问题应该是当前用单个ArrayList<Double>存储的方式没办法区分X和Y坐标对,导致后续距离计算无法进行。我来给你两种清晰的实现思路,帮你推进功能开发:
方案一:自定义Point类(推荐,可读性更强)
先定义一个专门存储坐标的Point类,让每个坐标对成为一个独立对象,这样存储和操作都会更清晰:
class Point { private double x; private double y; // 构造方法初始化坐标 public Point(double x, double y) { this.x = x; this.y = y; } // 获取X、Y坐标的方法 public double getX() { return x; } public double getY() { return y; } }
然后在主类中用ArrayList<Point>存储坐标,同时实现输入逻辑和距离计算:
import java.util.*; public class Assignment_12 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("name goes here"); System.out.println("You may quit at any time by typing \"Q\"."); // 用ArrayList存储Point对象,每个对象对应一个(X,Y)坐标对 ArrayList<Point> points = new ArrayList<>(); while (true) { System.out.print("请输入X坐标(输入Q退出):"); String xInput = input.nextLine(); if (xInput.equalsIgnoreCase("Q")) { break; } System.out.print("请输入Y坐标:"); String yInput = input.nextLine(); if (yInput.equalsIgnoreCase("Q")) { break; } // 处理输入转换,避免非数字输入报错 try { double x = Double.parseDouble(xInput); double y = Double.parseDouble(yInput); points.add(new Point(x, y)); System.out.println("已添加坐标(" + x + ", " + y + ")"); } catch (NumberFormatException e) { System.out.println("输入无效,请输入数字或Q退出。"); } } // 示例:计算前两个坐标之间的欧几里得距离 if (points.size() >= 2) { Point p1 = points.get(0); Point p2 = points.get(1); double distance = calculateDistance(p1, p2); System.out.printf("两点(%.2f,%.2f)和(%.2f,%.2f)之间的距离是:%.2f%n", p1.getX(), p1.getY(), p2.getX(), p2.getY(), distance); } else { System.out.println("存储的坐标不足两个,无法计算距离。"); } input.close(); } // 封装欧几里得距离计算逻辑 public static double calculateDistance(Point p1, Point p2) { double xDiff = p2.getX() - p1.getX(); double yDiff = p2.getY() - p1.getY(); return Math.sqrt(xDiff * xDiff + yDiff * yDiff); } }
方案二:用ArrayList<double[]>存储(简洁快速)
如果不想自定义类,可以直接用ArrayList<double[]>,每个数组元素存储两个值分别对应X和Y坐标:
import java.util.*; public class Assignment_12 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("name goes here"); System.out.println("You may quit at any time by typing \"Q\"."); ArrayList<double[]> points = new ArrayList<>(); while (true) { System.out.print("请输入X坐标(输入Q退出):"); String xInput = input.nextLine(); if (xInput.equalsIgnoreCase("Q")) { break; } System.out.print("请输入Y坐标:"); String yInput = input.nextLine(); if (yInput.equalsIgnoreCase("Q")) { break; } try { double x = Double.parseDouble(xInput); double y = Double.parseDouble(yInput); points.add(new double[]{x, y}); System.out.println("已添加坐标(" + x + ", " + y + ")"); } catch (NumberFormatException e) { System.out.println("输入无效,请输入数字或Q退出。"); } } // 计算前两个坐标的距离 if (points.size() >= 2) { double[] p1 = points.get(0); double[] p2 = points.get(1); double distance = calculateDistance(p1, p2); System.out.printf("两点(%.2f,%.2f)和(%.2f,%.2f)之间的距离是:%.2f%n", p1[0], p1[1], p2[0], p2[1], distance); } else { System.out.println("存储的坐标不足两个,无法计算距离。"); } input.close(); } public static double calculateDistance(double[] p1, double[] p2) { double xDiff = p2[0] - p1[0]; double yDiff = p2[1] - p1[1]; return Math.sqrt(xDiff * xDiff + yDiff * yDiff); } }
关键说明
你之前的代码用单个ArrayList<Double>存储所有数值,会导致X和Y坐标混在一起,无法对应成完整的坐标对。上面两种方案都解决了这个问题,把每个(X,Y)作为一个独立单元存储,这样后续计算距离时就能准确获取两个点的坐标值了。
内容的提问来源于stack exchange,提问作者SpaceGrape
相关产品推荐
相关产品推荐

