英文:
str.length; (vs) str.length()
问题
我在函数内部使用 length() 对字符串使用时遇到了“找不到符号”错误,但当我使用 length 时,错误消失了。我需要知道这背后的原因是什么?
代码:
//这里出现了“找不到符号”错误
static int[] matchingStrings(String[] strings, String[] queries) {
int n=queries.length();
int ns=strings.length();
int res[]=new int[n];
//这里却运行成功了
static int[] matchingStrings(String[] strings, String[] queries) {
int n=queries.length;
int ns=strings.length;
int res[]=new int[n];
英文:
I had a cannot find symbol error when I used length() for a string inside a function and got rid of it when i used length. I need to know what is the reason for this?
code:
//this got cannot find symbol error
static int[] matchingStrings(String[] strings, String[] queries) {
int n=queries.length();
int ns=strings.length();
int res[]=new int[n];
//this ran
static int[] matchingStrings(String[] strings, String[] queries) {
int n=queries.length;
int ns=strings.length;
int res[]=new int[n];
答案1
得分: 2
你感到困惑:
对于数组:array.length;
对于字符串:string.length();
对于数组,长度不被视为方法,而是一个常量(一个属性)(这就是为什么没有 ())。
英文:
You are confused:
For array: array.length;
For Strings: string.length();
For arrays, length is not considered a method, it is a constant (an attribute)(that's why there is no ()).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论