我寫了這個排序兩個數組,然後比較值,看看他們是否是相同的,但它總是返回false,我不明白爲什麼。我的代碼確定兩個數組是否是排列總是返回false,爲什麼?
它應該找出兩個數組是否是彼此的排列。
public class Permutations {
public static void main(String[] args) {
int[] a = {1,4,6,7,8,34};
int[] b = {34,6,8,1,4,7};
System.out.println(arePermutations(a, b));
}
public static boolean arePermutations(int[] a, int[] b)
{
int count = 0;
int temp = 0;
if(a.length == b.length)
{
for(int i=0; i<a.length-1; i++)
for(int j=0; j<a.length-1; j++)
if(a[i] > a[j+1] && i>j+1)
{
temp = a[i];
a[i] = a[j+1];
a[j+1] = temp;
}
{
for(int i=0; i<b.length-1; i++)
for(int j=0; j<b.length-1; j++)
if(b[i] > b[j+1] && i>j+1)
{
temp = b[i];
b[i] = b[j+1];
b[j+1] = temp;
}
}
for(int i=0; i<a.length; i++)
if(a[i] == b[i])
{
count++;
}
if (count == a.length)
{
return true;
}
else return false;
}
else return false;
}
}
您應該將排序分爲單獨的函數並自行測試。 – Ryan
請解釋一下你在這個程序中試圖做什麼的步驟。正如上面指出的那樣,用於分類和比較的單獨功能。 – denis
好的,謝謝你的回答。 –