2015-09-22 63 views
1

我很難完成爲學校完成此作業。除了標題中還有更多內容,但基本上這是我首先需要做的。閱讀具有x和y點的文本文件的每一行,提取它們,並將它們存儲在新節點中。然後,我需要將這個新節點添加到鏈接列表中,併爲每個xy座標重複。沒有數組將被使用。在LinkedList中存儲節點中的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鏈接列表類。

回答

0

你可以改變你的類點是這樣的:

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類的split()方法。

對於每一行,你會:

  • 拆呢
  • 從拆分結果
  • 獲取座標創建新的點與座標
  • 設置新的點作爲尾部爲前點
  • 你可以在主循環前一次做上述步驟,初始化第一個節點,鏈表的頭部
0

將LinkedList想象爲一個鏈接節點鏈,並且每個節點都包含您的數據或值(例如一個點)。當您開始解析.txt file中的座標時,開始製作節點並通過節點中的這些屬性鏈接它們,如"previous""next"。這些屬性也是節點類型,在C中表示爲指針。例如,在Java

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

你會發現這個link非常有幫助。

0

你的Point類將需要保存兩個座標,無論「重要性值」是什麼。沿此線的東西,用構造函數和方法並沒有什麼

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

編輯: OP評論說,他們不再需要實現鏈表類。我會在底部對任何感興趣的人在底部留下細節。

一旦設置完成,您就可以在您的ShapeAbstraction類中使用該類。您需要解析每一行以獲取座標並創建一個新的Point,然後將Point添加到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>(); 



你需要一些更多的類來處理實現自己的LinkedList。由於分配說:

您必須至少有類

  • 一個點。一個點包含位置x,y和重要性值。
  • a Node。鏈接列表的基本結構。它需要能夠保存一個Point,以及對下一個Node的引用。
  • 一個LinkedList。您的主要數據結構。您將需要添加點的方法來遍歷列表。您還必須能夠訪問列表中的元素。這裏有一個方法「getNext」,以及一個「reset」方法,它將「getNext」中使用的引用重置爲開始。

你然後有一個節點類,作爲書面記錄表示,需要持有一個點和下一個節點的引用。

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

而且你需要實現一個鏈表。還有其他的在線資源,例如this SO question。你需要有增加了一個點對象列表的方法:

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; 
} 

注:我的代碼沒有做任何錯誤檢查或空檢查或任何東西,我當然沒有寫的所有方法和構造函數,你需要編寫。希望這將有助於清理需要爲您做的事情!

+0

謝謝,這有助於很多。我忘了提及,我們不需要再實現我們自己的鏈表方法。我們現在可以使用java鏈接列表類。 –