为什么字符串比较失败?Java字符串比较失败
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'和'temp2'是'char []',并且在它们之间使用'equals'不起作用(它不会像你认为的那样)。试试这个:'if(Arrays.equals(temp,temp2))' – Jesper
究竟是什么问题?它在哪里失败?你的代码是做什么的?它应该做什么? –