str.length; (vs) str.length()

huangapple go评论63阅读模式
英文:

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 ()).

huangapple
  • 本文由 发表于 2020年8月18日 21:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63469812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定