Using Arrays Java -


my question is:

  • is code correct , assigned wheel speeds , duration
  • what need in order add main method run code
  • use loop iterate through each row, instructing finch robot move according numbers in each row. pause after each movement using code joptionpane.showmessagedialog(null,"click ok continue...");

public class fincharray {

public static final int left_wheel = 0; public static final int right_wheel = 1; public static final int duration = 2;{       int[][] x = new int[10][3]; // 2d array filled zeros     int time = 25;     (int[] row : x) {         row[2] = time;         time += 25;     }      (int[] row : x) {         row[duration] = time;         time += 25;     }      (int[] row : x){         row[left_wheel] = time;         time += 25;     }      (int[] row : x){         row[right_wheel] = time;         time += 25;     }      finch frobot = new finch();      frobot.setwheelvelocities(left_wheel,right_wheel,duration);     long before = system.currenttimemillis();     while (system.currenttimemillis() - before < 5000){           if(frobot.isobstacle())break; {          }             frobot.stopwheels();             frobot.quit();      }      } 

you can initialize 2d array (which, in java, array of 1d arrays) this:

int[][] x = new int[10][3]; // 2d array filled zeros int time = 25; (int[] row : x) {     row[2] = time;     time += 25; } 

this uses enhanced for loop , equivalent this:

int[][] x = new int[10][3]; // array filled zeros int time = 25; (int = 0; < x.length; ++i) {     int[] row = x[i];     row[2] = time;     time += 25; } 

note leaves left , right wheel speeds @ default values of zero.

your current code first creating 10x3 2d array, loop reassigning each row new 10-element array, end 10x10 array (which not want).

p.s. using 3-element array represent 3 distinct pieces of data not programming style (although work fine). slight improvement can obtained using symbolic constants subscripts. declare @ top of class:

public static final int left_wheel = 0; public static final int right_wheel = 1; public static final int duration = 2; 

then use these index each row:

for (int[] row : x) {     row[duration] = time;     time += 25; } 

since each row represents 3 different attributes, better approach define separate class contains data:

public class step {     public int leftwheelspeed;     public int rightwheelspeed;     public int duration; } 

then instead of 2d array of int values, can declare 1d array of step objects:

step[] x = new step[10]; int time = 25; (step step : x) {     step.duration = time;     time += 25; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -