java - Having trouble initializing an ArrayList of Objects inside a constructor -
hi have neuralnetwork class, constructor should initialize:
- a 2-dimensional double array, double[# of rows in .data file][# of numbers in each row]
- [# of numbers in each row] - 1 inputnodes
- 2/3 (rounded down) * # of hiddennodes
- 1 output node (which hiddennode object) takes in hiddennodes
what having trouble with
- having java create inputnodes, hiddennodes, , outputnode automatically based on data given, far have manually create nodes
the inputnodes stored in either array list or array, , passed constructor parameters of hiddennode, hiddennode can take parameters of:
hiddennode(node[] nodes)
node object superclass of hiddennode , inputnode
here code have far constructor:
neuralnetwork.java
/* * these values neuralnetwork constructor */ private final string comma = ","; private final string qmarks = "?"; private list<inputnode> input = new arraylist<inputnode>(); // input nodes private list<hiddennode> hiddennodelayer = new arraylist<hiddennode>(); // arraylist of hiddennode[] arrays private list<hiddennode> outputnode = new arraylist<hiddennode>(); public neuralnetwork(file f){ try { @suppresswarnings("resource") scanner infile = new scanner(f); int noofnodes; //while there line in infile. while (infile.hasnextline()){ //store line string line string line = infile.nextline(); //system.out.println(line);//test code see file looks //parition values separated comma string[] numbers = line.split(comma); //system.out.println(arrays.tostring(columns)); //test code see length of each column /*code works , prints out row * */ /* * initialize noofnodes length of numbers - 1 */ noofnodes = numbers.length - 1; //counter number of rows in .data file int noofrowsindata = 0; //this count number of rows in .data file linenumberreader lnr = new linenumberreader(new filereader(f)); try { lnr.skip(long.max_value); noofrowsindata = lnr.getlinenumber(); lnr.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } data = new double[noofrowsindata][numbers.length]; //system.out.println(data[0].length); //test code works properly, , prints numbers.length //copy on data form .data file (int = 0; < data.length; i++) { (int j = 0; j < data[i].length; j++) { // each row... if (!numbers[j].equals(qmarks)) { // if values in each row not equal "?" // set rows[i] values in column[i] data[i][j] = double.parsedouble(numbers[j]); //system.out.println(data[i][j]); //test code see what's printed here } else { data[i][j] = 0; //system.out.println(data[i][j]); //test code see what's printed here } } } } //system.out.println(data.length); //see length of data double array is. works //test code print out 2-d double array , see being stored // for(int = 0; < data.length; i++) { // system.out.println("for row[" + + "] of file:"); //works // (int j = 0; j < data[i].length; j++) { // system.out.println(" data[" + + "][" + // j + "] = " + data[i][j]); //works // } // } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } //create 13 inputnode objects initialized 0.0 (int n = 0; n < 13; n++){ input[n] = new inputnode(0.0); } //create 8 hiddennode objects **having problems here** (int o = 0; o < 8; o++){ hiddennodelayer.add(new hiddennode(input)); } system.out.println(hiddennodelayer.size() + " hiddennodes created"); //create 1 output node, hidden node outputnode = new arraylist<hiddennode>(1); system.out.println(outputnode.size() + " outputnode created"); }
update: nullpointerexception error
getting error here:
for (int m = 0; m < noofnodes; m++){ input[m] = new inputnode(0.0); //nullpointerexception error } system.out.println(input.length); //create 8 hiddennode objects **having problems here** int noofhiddennodes = (int)math.floor(2.*noofnodes/3.); system.out.println(noofhiddennodes); (int o = 0; o < noofhiddennodes; o++){ hiddennodelayer.add(new hiddennode(input)); }
found , corrected error
i had initialize input = new inputnode[m] in loop, because there no inputnodes begin with
for (int m = 0; m < noofnodes; m++){ input = new inputnode[m]; } system.out.println(input.length + ""); //create 8 hiddennode objects **having problems here** int noofhiddennodes = (int)math.floor(2.*noofnodes/3.); (int o = 0; o < noofhiddennodes; o++){ hiddennodelayer.add(new hiddennode(input)); } system.out.println(hiddennodelayer.size() + " hiddennodes created"); //create 1 output node, hidden node outputnode = new arraylist<hiddennode>(1); system.out.println(outputnode.size() + " outputnode created");
do mean this? (by way, hiddennodelayer defined?)
for (int n = 0; n < noofnodes; n++){ input[n] = new inputnode(0.0); } //create 8 hiddennode objects **having problems here** int noofhiddennodes = (int)math.floor(2.*noofnodes/3.); (int o = 0; o < noofhiddennodes; o++){ hiddennodelayer.add(new hiddennode(input)); }
update: given have defined input arraylist, can push new nodes there in same hiddennodelayer.
private list<inputnode> input = new arraylist<inputnode>(); // input nodes (int n = 0; n < noofnodes; n++){ input.add(new inputnode(0.0)); } //create 8 hiddennode objects **having problems here** int noofhiddennodes = (int)math.floor(2.*noofnodes/3.); (int o = 0; o < noofhiddennodes; o++){ hiddennodelayer.add(new hiddennode(input)); }
Comments
Post a Comment