java - Android getDeclaredFields() return a disorder result -


i declared class this:

public class student implements serializable {      private static final long serialversionuid = 8010508999597447226l;      public long id;     public string name;     public int age;     public string phone;     public string address;      public static void main(string[] args) {         field[] fields = student.class.getfields();         system.out.println("getfields(): " + fields2string(fields));         fields = student.class.getdeclaredfields();         system.out.println("getdeclaredfields(): " + fields2string(fields));     }      private static string fields2string(field[] fields) {         if (fields != null) {             stringbuilder sb = new stringbuilder();             (field f : fields) {                 sb.append(f.getname());                 sb.append(",");             }             return sb.tostring();         }         return null;     } } 

the main running result is: (the order same student.java, expected order)

getfields(): id,name,age,phone,address, getdeclaredfields(): serialversionuid,id,name,age,phone,address, 

but same code running on android (4.1.2 dalvik jvm), result fields order is:

07-28 09:54:14.271 5972-5972/com.ex i/system.out: getfields(): address,age,id,name,phone, 07-28 09:54:14.271 5972-5972/com.ex i/system.out: getdeclaredfields(): serialversionuid,address,phone,name,age,id, 

the order strange, not class order or alphabet order noted implementation of getdelclaredfields() in android changed (it's native method public native field[] getdeclaredfields();). may impossible change behavior. still want known how ordered fields result.

if need them in particular order can use arrays.sort(). in example below them sorted name (note i'm using lambda comparator):

arrays.sort(getclass().getdeclaredfields(), (a, b) -> a.getname().compareto(b.getname()));


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -