java - Unable to change the value of variable name? -


can change value inside compare method? error - variable need declared final, final wont allow me change. want compare other variables jsonarray(like total_transit_time, total_walking_time). cant think of solution that. teach me easier way it?

public  jsonarray findshortest(jsonobject json_object) throws jsonexception {     jsonarray sortedjsonarray = new jsonarray();     list<jsonobject> jsonlist = new arraylist<jsonobject>();     (int = 0; < json_object.length(); i++) {         int name = i;         jsonobject json_array = json_object.optjsonobject(""+name);         jsonlist.add(json_array);     }     system.out.println("jsonlist = " + jsonlist.tostring());      collections.sort(jsonlist, new comparator<jsonobject>() {          public int compare(jsonobject a, jsonobject b) {             string vala = new string();             string valb = new string();              try {                 vala = string.valueof(a.get("total_duration"));                 valb = string.valueof(b.get("total_duration"));             } catch (jsonexception e) {                 //do             }              return vala.compareto(valb);         }     }); 

to this

public  jsonarray findshortest(jsonobject json_object, string sortbythiselement) throws jsonexception {     ......              ......              try {                 vala = string.valueof(a.get(sortbythiselement));                 valb = string.valueof(b.get(sortbythiselement));             } catch (jsonexception e) {                 //do             }             ......         }     }); 

you can declare sortbythiselement final,then can use directly:

public  jsonarray findshortest(jsonobject json_object, final string sortbythiselement) throws jsonexception { ......          ......          try {             vala = string.valueof(a.get(sortbythiselement));             valb = string.valueof(b.get(sortbythiselement));         } catch (jsonexception e) {             //do         }         ......     } }); 

the other way is,create final variable in method,then visit in compare method:

public  jsonarray findshortest(jsonobject json_object, string sortbythiselement) throws jsonexception { ......          ......         system.out.println("jsonlist = " + jsonlist.tostring());         final string sortbythis = sortbythiselement;//note should add before collections.sort          ........         try {             vala = string.valueof(a.get(sortbythis));             valb = string.valueof(b.get(sortbythis));         } catch (jsonexception e) {             //do         }         ......     } }); 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -