![](/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.