![](/img/trans.png)
[英]Why can't I get any other output from my program through setOnClickListener?
[英]Why can't I get any output from my recursive method?
我正在尝试插入字符 (a, b, c) 并获取数组的排列。
由于某种原因,它没有打印出来。 我确定这是一个简单的错误,但我找不到它。 非常感谢您的建议。
public static void main(String[] args) {
int [] A = new int []{'a','b','c'};
permute(A, 3);
}
public static void permute(int[] A, int p){
if(A.length == 0){
return;
}
for(int i = 0; i < A.length; i++){
char ch = (char) p;
p += A[i];
permute(A,p);
p = ch;
}
}
你的方法有几个问题:
int
时使用char
,反之亦然;System.out.print
语句,因此您永远不会指示 Java 程序打印任何内容;for
部分并不断构建调用堆栈; 和p
是什么意思。直列排列的程序是这样的:
public static void permute(char[] a, int p){
if(p >= a.length) {//we've reached the end of the array, now print
for(int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
System.out.println();
} else {
char cp = a[p];
for(int i = p; i < a.length; i++){
char ci = a[i];
a[p] = ci;
a[i] = cp;
permute(a,p+1);
a[i] = ci;
}
a[p] = cp;
}
}
然后调用该方法permute(a,0)
与a
你想排列替换字符的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.