我正在为我的班级做一个作业,我需要为路径创建下面的函数。我在功能上遇到了麻烦,我是不是想多了?

I'm doing an assignment for my class where I need to create the functions below for a Path. I'm having trouble on the functions, am I overthinking it?

提问人:nefiaragon 提问时间:11/14/2022 更新时间:11/17/2022 访问量:17

问:

import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;

public class Path 
{
    ArrayList<Point> pointOne;
    ArrayList<Point> pointTwo;
    
    public Path() {
        pointOne = new ArrayList<Point>();
        pointTwo = new ArrayList<Point>();
    }
    
    public Path(Scanner s) 
    {
        pointOne = new ArrayList<Point>();
        pointTwo = new ArrayList<Point>();
        
        
    }
    
    public int getPointCount()
    {
        return 0;
    }
    
    public int getX(int n)
    {
        return n;
    }
    
    public int getY(int n)
    {
        return n;
    }
    
    public void add(int x, int y)
    {
        
    }
    
    public String toString()
    {
        
    }

到目前为止,这就是我对 getX 和 getY 函数的了解,如果我没有做正确的事情,请随时纠正我。

我尝试研究一些不同的方法来将两个点添加到数组列表中。我在这里找到了一个,但它并没有我想象的那么有帮助。我也对如何使用扫描仪扫描点并建立路径感到困惑。我只是愚蠢和过度思考吗?我要和我的老师谈谈,看看他是否能解决任何问题,但任何帮助将不胜感激,谢谢

Object ArrayList 构造函数 java.util.scanner point

评论

0赞 Cole Henrich 11/17/2022
如果您发现某个答案有帮助,请接受(点击复选)并投赞成票。

答:

0赞 Cole Henrich 11/17/2022 #1
public class Main {
    public static void main {
        ArrayList<Point> path = new ArrayList<Point>();
        path.add(new Point(x1,y1));
        path.add(new Point(x2,y2));
    }
}
public class Point {
    private double X;
    private double Y;
    public Point(double x, double y){
       X = x;
       Y = y;
  }
  public double getX(){return X;}
  public double getY(){return Y;}
}

你看,路径是一个点列表,从概念上讲,具体来说,这里是一个点的 ArrayList。Point 是一种具有属性 x 和 y 的对象。您不需要创建名为 Path 的类型,因为 Path 只是一个 .你不能扩展 ArrayLists,可能你还不知道如何继承。但是,除非你被明确告知要这样做,否则没有必要将 Path 变成一个类。ArrayList<Point>

此外,Point 可以作为一个类型删除,而只是作为一个 .从概念上讲,点只是它在每个方向上的数字坐标列表。ArrayList<Double>

因此,路径可以表示为 .ArrayList<ArrayList<Double>>