Creating an unknown number of Nodes Java -


i'm working on program implements dijkstra's algorithm on series of vertices , edge weights input user. not know how many vertices user going input until input data. (one of first input values total number of vertices) problem not know how create node each vertex without hard coding such as:

vertex v1 = new vertex("vertex info"); vertex v2 = new vertex("vertex info"); vertex v3 = new vertex("vertex info"); 

etc.

to more specific, basing code off of example here. in main method code has vertex nodes hard coded.

this i'd written on obtaining user input on previous attempt (it discarded because ran issues down line , started over)

    arraylist leftpoints = new arraylist(); arraylist rightpoints = new arraylist(); arraylist weights = new arraylist();     scanner input = new scanner(system.in);  string searchinput = input.nextline();      string[] searchcommand = searchinput.split(" ");     for(int = 0; <searchcommand.length; i++){         system.out.print(searchcommand[i] + " ");     }          searchinput = input.nextline();         int totalvertices = integer.parseint(searchinput);          while (input.hasnextline()){                     searchinput = input.nextline();          searchcommand = searchinput.split(" ");                      int x = integer.parseint(searchcommand[0]);         int y = integer.parseint(searchcommand[1]);         int weight = integer.parseint(searchcommand[2]);          leftpoints.add(x);         rightpoints.add(y);         weights.add(weight);          }          int[] x = new int[leftpoints.size()];         int[] y = new int[rightpoints.size()];         int[] pathweights = new int[weights.size()];          (int p = 0; p < x.length; p++){             x[p] = ((integer) leftpoints.get(p)).intvalue();             y[p] = ((integer) rightpoints.get(p)).intvalue();             pathweights[p] = ((integer) weights.get(p)).intvalue();         } 

i converted arraylists integer arrays attempt did not work @ all, decided leave in code in case may me here

create vertices user inputs them, , add them list.

scanner sc = new scanner(system.in); list<vertex> vertexlist = new linkedlist<>();  while(sc.hasnext()) {     vertexlist.add(createvertexfromstring(sc.next()); } 

createvertexfromstring method write yourself, obviously.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -