i getting battery values drone. able display new battery value on jlabel. however, when trying store these battery values int array, store first battery value on array. subsequent array values fill first battery value.
i show output understand happening. first value getting drone while second value indicate array index. output show array cannot accept new data unknown reason.
p/s: have no idea best size of array since getting values drone every seconds. have declared int array size of 9999999. idea how can set array max size cater needs of getting continuous battery values drone? values being used drawing graph later.
my code:
public class ardroneframe extends javax.swing.jframe implements runnable, dronestatuschangelistener, navdatalistener { private string text; // string speech private static final long connect_timeout = 10000; public ardrone drone; public navdata data; public timer timer = new timer(); public int batterygraphic=0; public int [] arraybatt = new int[9999999]; public ardroneframe(string text) { this.text=text; } public ardroneframe() { initcomponents(); initdrone(); } private void initdrone() { try { drone = new ardrone(); data = new navdata(); } catch (unknownhostexception ex) { return; } videodrone.setdrone(drone); drone.addnavdatalistener(this); } public void navdatareceived(navdata nd) { getnavdata(nd); int battery = nd.getbattery(); cmdlistok.jlblbatterylevelvalue.settext(battery + " %"); //jlabel can updated & display new battery values } public void getnavdata(navdata nd){ for(int i=0;i<arraybatt.length;i++){ batterygraphic= nd.getbattery(); arraybatt[i] = batterygraphic; system.err.println("this stored battery values : " + arraybatt[i] + " " + + "\n"); } } } public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { public void run() { string text = "welcome!"; ardroneframe freetts = new ardroneframe(text); freetts.speak(); new ardroneframe().setvisible(true); } }); } result:
this stored battery values : 39 0 stored battery values : 39 1 stored battery values : 39 2 stored battery values : 39 3 stored battery values : 39 4 stored battery values : 39 5
the problem lies in method:
public void getnavdata(navdata nd){ (int batteryvalue : arraybatt){ arraybatt[i] = nd.getbattery(); system.err.println("this stored battery values : " + arraybatt[i] + " " + + "\n"); } } you call method passing navdata instance. means whatever value nd contains nd.getbattery() being assigned every index in array loop interates on battery array.
what should do, move loop outside of getnavdata(navdata nd) method, , pass new instance of navdata each call. when couple arraylist suggestion below, should have dynamic array of distinct battery values
side solution
the way have declared array really scary. should use space need , nothing more. know unsure of size required, don't go over-board on it.
you should initialize array smaller;
public int [] arraybatt = new int[10000]; as side note: having class members public not recommended. should make them private , create getter/setter methods retrieve , modify data, respectively.
then, have method checks see if array full. if full, increase array size n/2, n initial size of array.
the down-side approach array becomes larger, going spend lot of time copying old array new array, pretty undesirable.
a better solution
would use built-in arraylist library, append items list , let java heavy lifting.
arraylist<integer> batteryarray = new arraylist <integer>(); you can add items list calling:
batteryarray.add(item); the upside solution that:
- the batteryarray size handled behind-the-scenes
- the size of array retrievable, elements
- arraylist fast storage structure.
in loop print out battery values, make lot cleaner implementing for-each loop.
why using
system.errprint out dialogs battery?? isn'tsystem.errmeant used , violates principle of least astonishmentpublic void getnavdata(navdata nd){ (int batteryvalue : arraybatt){ arraybatt[i] = nd.getbattery(); system.err.println("this stored battery values : " + arraybatt[i] + " " + + "\n"); }}
Comments
Post a Comment