使用org.reflections API来发现它不适用于枚举:
List<ClassLoader> classLoadersList = new LinkedList<>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(new org.reflections.util.ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("my.package"))));
Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Enum.class);
// Above line should've returned all enums recursively, doesn't work!
还有其他方法吗?
爪哇
递归实现枚举模板:
public class DemoFangeTInshu {
/**
* @param args
*/
static int count=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] data = "0123456789".toCharArray();
int[] da = new int[11];
int t=10;
while(t-->0){
da[t]=(int)data[t]-'0';
System.out.println(da[t]);
}
dfs(da,0,9);
System.out.println(count);
}
private static void dfs(int[] da, int a, int length) {
// TODO Auto-generated method stub
if(a==length){
if(check(da)){
count++;
}
}
else{
for(int i=a;i<=length;i++){
swap(da,i,a);
dfs(da,a+1,length);
swap(da,i,a);
}
}
}
private static void swap(int[] da, int i, int a) {
// TODO Auto-generated method stub
int temp=da[i];
da[i]=da[a];
da[a]=temp;
}
private static boolean check(int[] da) {
// TODO Auto-generated method stub
if(**条件**) //把之间的条件用放在此处
return false;
else return true;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。