尺寸和长度方法的区别? [英] Difference between size and length methods?

查看:25
本文介绍了尺寸和长度方法的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.size().length 有什么区别?.size() 仅用于数组列表而 .length 仅用于数组吗?

What is the difference between .size() and .length ? Is .size() only for arraylists and .length only for arrays?

推荐答案

size()java.util.Collection 中指定的一个方法,然后被继承标准库中的每个数据结构.length 是任何数组上的一个字段(数组是对象,你只是通常看不到这个类),而 length()java 上的一个方法.lang.String,无论如何它只是 char[] 的一个薄包装.

size() is a method specified in java.util.Collection, which is then inherited by every data structure in the standard library. length is a field on any array (arrays are objects, you just don't see the class normally), and length() is a method on java.lang.String, which is just a thin wrapper on a char[] anyway.

也许是设计使然,字符串是不可变的,并且所有顶级 Collection 子类都是可变的.所以你看到长度"的地方你知道它是恒定的,而你看到大小"的地方不是.

Perhaps by design, Strings are immutable, and all of the top-level Collection subclasses are mutable. So where you see "length" you know that's constant, and where you see "size" it isn't.

这篇关于尺寸和长度方法的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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