我写了这个排序两个数组,然后比较值,看看他们是否是相同的,但它总是返回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
好的,谢谢你的回答。 –