何时使用。长度VS。长度() [英] When to use .length vs .length()

查看:94
本文介绍了何时使用。长度VS。长度()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有这个code:

Ok so I have this code:

public class Test {
    public static void main(String[] args) {

        String[] people = {"Bob", "Billy", "Jim"};
        int sum = 0;

        for(int i = 0; i < people.length; i++) {
            sum += people[i].length();
        }
    }
}

我的问题是,为什么人[I]。长度()。长度(),而不是。长度。我以为让你使用一个数组的长度。长度,而不是。长度()。你使用。长度(),因为你试图得到一个字符串的长度,而不是一个数组?附:我是一个初学者,所以这似乎很明显你,但它不是我的。

My question is why people[i].length() is .length() and not .length. I thought to get the length of an array you use .length and not .length(). Do you use .length() because you are trying to get the length of a string and not an array? P.S. I am a beginner, so this may seem very obvious to you, but it isn't to me.

推荐答案

他们是两个完全不同的东西。

They're two completely different things.

。长度上的阵列属性即可。这不是一个方法调用。

.length is a property on arrays. That isn't a method call.

。长度()字符串

您所看到的这既是因为第一,你遍历数组的长度。数组的内容字符串,并要添加的所有它们的长度,所以你再呼吁每一个人字符串的长度阵列中的

You're seeing both because first, you're iterating over the length of the array. The contents of the array are String, and you want to add up all of their lengths, so you then call the length on each individual String in the array.

这篇关于何时使用。长度VS。长度()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆