为什么这个字符串比较失败?
package javaapplication57;
/**
*
* @author amit
*/
public class JavaApplication57 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String a = "anagram";
String b = "margana";
int len = a.length();
char[] temp = a.toCharArray();
char[] temp2 = b.toCharArray();
int len2 = b.length();
for(int j = 0; j<len-1; j++)
{
for(int i = 0; i<len-1; i++)
{
if(temp[i]>temp[i+1])
{
char t = temp[i];
temp[i] = temp[i+1];
temp[i+1] = t;
}
}
}
//System.out.println(temp);
for(int j = 0; j<len2-1; j++)
{
for(int i = 0; i<len2-1; i++)
{
if(temp2[i]>temp2[i+1])
{
char t = temp2[i];
temp2[i] = temp2[i+1];
temp2[i+1] = t;
}
}
}
//System.out.println(temp2);
if(temp.equals(temp2))
{
System.out.println("yes");
}
}
}
temp
andtemp2
arechar[]
and usingequals
between them does not work (it does not do what you think it does). Try this instead:if (Arrays.equals(temp, temp2))
- Jesper