在 LinkedList 的节点中存储 X、Y 边界坐标

Store X, Y Boundary Coordinates in Node for LinkedList

我很难为学校完成这项作业。除了标题中的内容之外,还有更多内容,但基本上这是我首先需要做的。读取包含 x 和 y 点的文本文件的每一行,提取它们并将它们存储在新节点中。然后我需要将这个新节点添加到链表中并为每个 x y 坐标重复。不使用数组。

这是我目前的代码:

public class Point {

public int data;
public Point next;

public Point(int data, Point next) {
    this.data = data;
    this.next = next;
}

@Override
public String toString() {
    return data + "";
}

}

public class ShapeAbstraction {

public void readCoordinates() {
    String fileName = "shapelist.txt";
    String line = null;

    try {
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {

            System.out.println(line);
        } 
        bufferedReader.close();
    }

    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file " + fileName);
    }

    catch(IOException ex) {
        System.out.println("Error reading file" + fileName);
    }

}

public static void main(String[] args) {
    ShapeAbstraction sa = new ShapeAbstraction();
    sa.readCoordinates();

    Point front = new Point(0, null);
    System.out.println(front);
}

}

.txt 文件中的坐标如下所示

10 0
18 0
17 2
21 7
19 12

这只是数百个中的前 5 个。

如果有人能提供帮助那就太好了。谢谢。 Assignment for reference 顺便说一句,我们现在可以使用 java 链表 class.

将 LinkedList 视为链接节点的链,每个节点都包含您的数据或值(例如一个点)。当您开始解析 .txt file 中的坐标时,开始创建节点并通过 "previous""next" 等节点中的属性将它们链接起来。这些属性也是节点类型,在 C 中表示为指针。例如 Java:

private class Node {
   private Point value;
   private Node previous;
   private Node next;
}

您可能会发现此 link 非常有帮助。

您的 Point class 将需要保存两个坐标,无论“重要性值”是什么。与此类似的东西,有构造函数和方法,还有什么不是

private class Point {
    int x;
    int y;
    int importance;
    //Add constructors/methods that are needed
}

编辑: OP 评论说他们不再需要实现链表 class。对于任何感兴趣的人,我会在底部留下我的详细信息..

设置完成后,您就可以在 ShapeAbstraction class 中使用 class。您需要解析每一行以获取坐标并创建一个新点,然后将该点添加到 LinkedList。大致如下:

int x = line.split(" ")[0];
int y = line.split(" ")[1];
Point newP = new Point(x, y); //assuming you create this constructor
allPoints.add(newP); //where allPoints was created with new LinkedList<Point>();



您还需要一些 classes 来处理实现您自己的 LinkedList。正如作业所说:

You must at least have classes for

  • a Point. A point contains the position x,y, and the importance value.
  • a Node. The basic structure for your linked list. It needs to be able to hold a Point, as well as the reference to the next Node.
  • a LinkedList. Your main data structure. You will need methods to add a point, to traverse the list. You also must be able to access elements in the list. Here it would make sense to have a method "getNext", along with a method "reset", which resets the reference used in "getNext" to the start.

然后您将拥有一个节点 class,正如文章所说,它需要保存一个点和对下一个节点的引用。

private class Node {
    Point point;
    Node next;
    //Add constructors/methods that are needed
}

并且您需要实现一个链表。还有其他在线资源,例如 this SO question。您需要有一个方法将 Point 对象添加到列表中:

public void add(Point p) {
    Node newN = new Node(p); //assuming you've created this constructor

    //Then add the point, depending on how you implement the linked-list.
    //It might look something like:
    last.next = newN;
    last = newN;
}

注意:我的代码没有做任何错误检查或空值检查或任何事情,我当然没有编写您需要编写的所有方法和构造函数。希望这会帮助您弄清楚需要为您做些什么!

您可以这样更改您的 class 点:

public class Point {

public int x;
public int y;
public Point tail;

public Point(int x, int y) {
    this.x = x;
    this.y = y;
}

public void setTail (Point tail) {
    this.tail = tail;
}

public Point getTail () {
    return this.tail;
}

@Override
public String toString() {
    return "(" + x + "," + y + ")";
}

}

当您从文件中读取行时,您可以使用 String class 的 split() 方法。

对于每一行,您将:

  • 拆分它
  • 从分割结果中获取坐标
  • 使用坐标
  • 创建一个新点
  • 将新点设置为前一个点的尾部
  • 可以在主循环之前做一次以上步骤,初始化第一个节点,链表的Head