java - Adding a cycle numbre to the name of an object -
i have code in java
for (int j = 0; j < 8; j++) { boton[1][j].seticon(peonn); peon peonnegro = new peon('n'); boton[6][j].seticon(peonb); }
this chess, want each new object have number of loop use independently without creating array, have like
for (int j = 0; j < 8; j++) { boton[1][j].seticon(peonn); peon peonnegro+i = new peon('n'); boton[6][j].seticon(peonb); }
so i'll have peonnegro0, peonnegro1 , on...
you aren't going able without array or collection
. (in java, quite difficult use dynamic variable name). you'll have declare array or arraylist
outside of loop, shown below.
peon[] peons = new peon[8]; (int j = 0; j < 8; j++) { boton[1][j].seticon(peonn); peons[j] = new peon('n'); boton[6][j].seticon(peonb); } // can access single peon peon p3 = peons[3]; // or iterate on peons , cycle number (int cycle_num = 0; cycle_num < 8; cycle_num++) { peon peon = peons[cycle_num]; // peon , cycle_num here }
Comments
Post a Comment