java - Applying LinkedList via Method -
i learning how use linkedlist , worked fine if use direct approach (without using method), had many errors.
what want is, read file text , save linkedlist.
this have far:
public static void main(string[] args) { node<string> workflowhead = null; node<string> workflowtail = null; try { int = 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()); } } the above meant 'direct approach' without using method.
now, tell me, how achieve of using method?
the above code works fine need convert method-using code.
i tried fails miserably:
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 = 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()); } } what did wrong?
one problem comes in when try head = ... or tail = ... (which won't generate compile-time errors or errors in current code, once start extending test it). each of these give local variable head or tail new value. won't change workflowhead or workflowtail.
on more technical level, when pass in object parameter, both parameter , original object point same data. modifying object change both. however, when reassign parameter =, parameter point different object while original still point original object.
what want class variables rather method parameters.
also, workflowhead.read(...) won't work because read member of method class (probably not ideally named) wordflowhead of type node.
it may better idea pass in file name rather hard-code in method. 'better' way passing in command-line, but, testing purposes, not ideal.
i this:
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) { // ... - can freely reassign head , tail here } // i'm assuming class defined elsewhere, // defining here make more sense private class node<t> { // ... } } it might better put entire read method mylinkedlist constructor.
if above beyond current understanding, may more understandable:
public static void main(string[] args) { read("workflow.txt"); } static node<t> head = null, tail = null; // these class variables static void read(string filename) { // ... scanner in = new scanner(new fileinputstream(filename)); // ... - can freely reassign head , tail here }
Comments
Post a Comment