2 回答
TA贡献1780条经验 获得超5个赞
使用String.valueOf一个字符数组转换成字符串:
public class Main {
public static void main(String[] args) {
char[] chars = {'f', 'o', 'x'};
String s1 = "fox";
String s2 = "not fox";
System.out.println(s1.equals(String.valueOf(chars)));
System.out.println(s2.equals(String.valueOf(chars)));
}
}
TA贡献1772条经验 获得超5个赞
将字符数组转为字符串,或将字符串转为字符数组:
char[] chars = {'a', 'b', 'c'};
String asString = new String(chars);
char[] back = asString.toCharArray();
要检查 2 个字符数组是否相等:
java.util.Arrays.equals(charArray1, charArray2)
要检查 2 个字符串是否相等:
string1.equals(string2);
请注意,这不起作用:
charArray1 == charArray2 // does not work. This checks if they are references to the same array, NOT if the contents are equal
string1 == string2 // also does not work; same reason.
添加回答
举报