通过方法应用LinkedList

时间:2013-05-15 08:32:00

标签: java methods linked-list

我正在学习如何最近使用LinkedList并且一切正常但如果我将其用作直接方法(不使用方法),则会出现许多错误。

我想要做的是,读取文件文本并将其保存到LinkedList中。

这是我到目前为止所做的:

public static void main(String[] args) {

    Node<String> workflowHead = null;
    Node<String> workflowTail = null;

    try {
        int i = 0;
        Scanner in = new Scanner(new FileInputStream("workflow.txt"));
        while (in.hasNextLine()) {
            if (i == 0) {
                workflowHead = new Node<String>(in.nextLine());
                workflowTail = workflowHead;
            }
            else {
                workflowTail.next = new Node<String>(in.nextLine());
                workflowTail = workflowTail.next;
            }
            i++;
        }
        in.close();
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
}

以上是我所说的'直接接近'而不使用方法。

现在,请告诉我,如何通过使用方法实现所有这些?

上面的代码工作正常,但我需要将其转换为使用方法的代码。

我尝试过这样但却失败了:

public static void main(String[] args) {

    Node<String> workflowHead = null;
    Node<String> workflowTail = null;

    workflowHead.read(workflowHead, workflowTail);
} //End of main

public class Method {

public void read(Object head, Object tail) {
    try {
        int i = 0;
        Scanner in = new Scanner(new FileInputStream("workflow.txt"));
        while (in.hasNextLine()) {
            if (i == 0) {
                head = new Node<String>(in.nextLine());
                tail = head;
            }
            else {
                tail.next = new Node<String>(in.nextLine());
                tail = tail.next;
            }
            i++;
        }
        in.close();
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

当您尝试执行head = ...tail = ...时会遇到一个问题(这不会在当前代码中生成任何编译时错误或错误,但是一旦您开始将其扩展到测试它)。这些中的每一项都只是为本地变量headtail提供一个新值。它不会更改workflowHeadworkflowTail

在技术层面上,当您传入Object参数时,参数和原始对象都将指向相同的数据。所以修改该对象将改变这两者。但是,当您使用= 重新分配参数时,参数现在将指向其他对象,而原始对象仍将指向原始对象。

你可能想要的是类变量而不是方法参数。

此外,workflowHead.read(...)无效,因为readMethod类的成员(可能没有理想的名称)但wordflowHead的类型为Node }。

传递文件名而不是在方法中对其进行硬编码也是一个更好的主意。 “更好”的方法是从命令行传递它,但是,出于测试目的,这并不理想。

我可能会这样做:

public static void main(String[] args) {
   MyLinkedList<String> linkedList = new MyLinkedList<String>();
   linkedList.read("workflow.txt");
}

static class MyLinkedList<T>
{
   private Node<T> head = null, tail = null;
   public void read(String filename)
   {
     // ... - you can freely reassign head and tail here
   }

   // I'm assuming this class is defined elsewhere,
   //   but defining it here would likely make more sense
   private class Node<T>
   {
     // ...
   }
}

将整个read方法放入MyLinkedList构造函数中可能会更好。

如果上述内容超出您目前的理解,这可能更容易理解:

public static void main(String[] args) {
   read("workflow.txt");
}

static Node<T> head = null, tail = null; // these are class variables
static void read(String filename)
{
   // ...
   Scanner in = new Scanner(new FileInputStream(filename));
   // ... - you can freely reassign head and tail here
}