使用 toCharArray 时此处不允许使用 Void 类型?
这段代码的目的是比较两个字符串中的字符,看看它们是否相同。它通过获取字符串,将它们转换为字符数组,对它们进行排序,然后比较它们来实现这一点。
private boolean sameChars(String firstStr, String secondStr)
{
return Arrays.equals(Arrays.sort(firstStr.toCharArray()), Arrays.sort(secondStr.toCharArray()));
}
当我编译此代码时,它会突出显示 (firstStr.toCharArray())
并显示 此处不允许“void”类型
。是什么导致了该错误以及如何修复它?
This code is meant to compare the characters in two strings and see if they are the same. It does so by taking the strings, converting them to a char array, sorting them, and then comparing them.
private boolean sameChars(String firstStr, String secondStr)
{
return Arrays.equals(Arrays.sort(firstStr.toCharArray()), Arrays.sort(secondStr.toCharArray()));
}
When I compile this code, it highlights (firstStr.toCharArray())
and says 'void' type not allowed here
. What's causing the error and how would I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Arrays.sort()
不返回数组。您需要将字符数组字符串存储到局部变量,然后对每个变量调用 sort,最后使用 Arrays.equals() 比较两个数组:Arrays.sort()
doesn't return the array. You'll need to store the character array strings to local variables, then call sort on each variable, and then finally compare the two arrays usingArrays.equals()
:Arrays.sort 返回 void。您无法将其“值”传递给 Arrays.equal。它就地排序。
Arrays.sort returns void. You can't pass its 'value' to Arrays.equal. it sorts in place.
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort%28char[]%29
你应该引用两个 char[] a, b; Array.sort a,Array.sort b 并返回 Array.equals a,b
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort%28char[]%29
you should reference two char[] a, b; Array.sort a, Array.sort b and return the Array.equals a,b