public enum HeaderEnum { BookName("Book Name"), ISBN("Isbn"), Title("Title"); private final static HeaderEnum [] columns = new HeaderEnum []{ BookName, ISBN, Title }; private final static String[] names = new String[columns.length]; static{ for(int i=0;i<names.length;i++){ names[i] = columns[i].headValue; } } public static int getSize(){ return names.length; } public static String[] toStringArray(){ return names; } public static HeaderEnum getColumn(int i){ try{ return columns[i]; }catch(Exception ex){ ex.printStackTrace(); return null; } } private String headValue; private HeaderEnum (String headValue) { this.headValue = headValue; } }
使用时这样
for(i=0;i<10;i++){ int columnIndex = i; HeaderEnum column = HeaderEnum.getColumn(columnIndex); switch (column) { case ISBN: returnValue = String.valueOf(s.getTradeId()); break; case TITLE: returnValue = s.getTopAcct(); break; } }