我用java编写了三个函数test1()
,test2()
和test3()
。我想编写一些代码来迭代并将它们调用到所有不同的组合中。
例如
test1()
,然后执行test2()
,最后执行test3()
,test2()
,test1()
,最后执行test3()
e.t.c。是否有可能通过迭代器执行此操作,或者我必须手动执行此操作?
答案 0 :(得分:9)
您可以使用反射来获取方法数组,然后遍历列表的每个permutation并一次调用一个。{p>看看JavaDoc for Class.getDeclaredMethods()。这是一个简单的例子:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class SO {
public void test1() {
System.out.println("Running test1");
}
public void test2() {
System.out.println("Running test2");
}
public void test3() {
System.out.println("Running test3");
}
public void notATest() {
System.err.println("THIS IS NOT A TEST");
}
public static void main(String ... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class c = SO.class;
SO that = new SO();
Method[] methods = filter(c.getDeclaredMethods(), "test");
PermuteMethod permutations = new PermuteMethod(methods);
while(permutations.hasNext()) {
for(Method permutation: permutations.next()) {
permutation.invoke(that, null);
}
System.out.println("----");
}
}
private static Method[] filter(Method[] declaredMethods, String startsWith) {
List<Method> filtered = new ArrayList<>();
for(Method method : declaredMethods) {
if(method.getName().startsWith(startsWith)) {
filtered.add(method);
}
}
return filtered.toArray(new Method[filtered.size()]);
}
}
这会产生follwoung输出(而不是它从不调用notATest() method
:
Running test1
Running test2
Running test3
----
Running test1
Running test3
Running test2
----
Running test2
Running test1
Running test3
----
Running test2
Running test3
Running test1
----
Running test3
Running test1
Running test2
----
Running test3
Running test2
Running test1
----
这是使用this permute class的以下(修改)版本:
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class PermuteMethod implements Iterator<Method[]> {
private final int size;
private final Method[] elements; // copy of original 0 .. size-1
private final Method[] ar; // array for output, 0 .. size-1
private final int[] permutation; // perm of nums 1..size, perm[0]=0
private boolean next = true;
public PermuteMethod(Method[] e) {
size = e.length;
elements = new Method[size];
System.arraycopy(e, 0, elements, 0, size);
ar = new Method[size];
System.arraycopy(e, 0, ar, 0, size);
permutation = new int[size + 1];
for (int i = 0; i < size + 1; i++) {
permutation[i] = i;
}
}
private void formNextPermutation() {
for (int i = 0; i < size; i++) {
Array.set(ar, i, elements[permutation[i + 1] - 1]);
}
}
public boolean hasNext() {
return next;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
private void swap(final int i, final int j) {
final int x = permutation[i];
permutation[i] = permutation[j];
permutation[j] = x;
}
public Method[] next() throws NoSuchElementException {
formNextPermutation(); // copy original elements
int i = size - 1;
while (permutation[i] > permutation[i + 1])
i--;
if (i == 0) {
next = false;
for (int j = 0; j < size + 1; j++) {
permutation[j] = j;
}
return ar;
}
int j = size;
while (permutation[i] > permutation[j])
j--;
swap(i, j);
int r = size;
int s = i + 1;
while (r > s) {
swap(r, s);
r--;
s++;
}
return ar;
}
}
答案 1 :(得分:2)
要获得所有排列,您可以使用Guava
项目。请查看Collections2课程。
然后使用内省来调用对象上的方法。
答案 2 :(得分:1)
可以使用嵌套循环来实现它。 在最内层循环中,调用基于循环计数器的值