Skip to content

Instantly share code, notes, and snippets.

@phl0w
Created June 23, 2012 14:58
Show Gist options
  • Select an option

  • Save phl0w/2978578 to your computer and use it in GitHub Desktop.

Select an option

Save phl0w/2978578 to your computer and use it in GitHub Desktop.
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class FieldsToArray {
public int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 0,
j = 0, k = 0, l = 0;
public static void main(String[] args) {
FieldsToArray fta = new FieldsToArray();
List<Field> array = getList();
for (Field f : array) {
try {
System.out.println((Integer) f.getInt(fta));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
public static List<Field> getList() {
List<Field> toReturn = new ArrayList<Field>();
Field[] fields = FieldsToArray.class.getDeclaredFields();
for (Field f : fields) {
if (f.getType().isPrimitive()) {
toReturn.add(f);
}
}
return toReturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment