长度和长度的区别是什么?(复制)

时间:2021-04-26 22:19:45

This question already has an answer here:

这个问题已经有了答案:

I've noticed that when doing the length of an array you write something like:

我注意到当做一个数组的长度时,你会这样写:

arrayone.length;

However, for such things as array lists or a string, you write a bracket on the end, such as the following for the length of the String:

但是,对于诸如数组列表或字符串之类的东西,您可以在末尾写一个括号,比如字符串长度如下:

stringone.length();

What is the key reason for this and how do you know when to put the brackets or now?

这其中的关键原因是什么?你怎么知道什么时候应该加上括号呢?

8 个解决方案

#1


8  

.length;

directly accesses a field member.

直接访问字段成员。

.length();

invokes a method (i.e. an accessor) to access a field member.

调用方法(即访问器)来访问字段成员。

in the case of String, this is to be expected since it's immutable.

对于字符串,这是可以预料的,因为它是不可变的。

#2


3  

Arrays are handled differently than Strings or ArrayLists or anything else that can be counted in Java. An Array is pretty much a native type and it's length can never be changed after it is initialized, so there's no need for encapsulation. The length variable can be directly exposed with no side effects.

数组的处理方式与字符串、数组列表或其他可以在Java中计数的东西不同。数组基本上是一个本机类型,它的长度在初始化后永远不会更改,因此不需要封装。长度变量可以直接暴露,没有副作用。

The reason why String uses a method instead of a variable is because it internally uses a char[] that it doesn't want to expose publicly (for immutability/encapsulation), so it wraps the length variable in a length() method. It's the same reason ArrayList has a size() method instead of a length variable.

String之所以使用方法而不是变量,是因为它在内部使用了一个char[],而它不希望公开(为了不变性/封装),所以它将length变量封装在一个length()方法中。这也是ArrayList使用size()方法而不是长度变量的原因。

The only time you'll use the variable instead of the method is with arrays. Everything else will be methods. That is, you'll use the brackets for everything except arrays.

使用变量而不是方法的惟一时间是使用数组。其他都是方法。也就是说,除了数组之外,所有东西都要使用方括号。

#3


2  

The only true way to know when to use which one is experience. Though an IDE with autocompletion will usually help you out when you don't remember.

知道何时使用哪一个的唯一正确方法是经验。虽然带有自动完成功能的IDE通常会在你不记得的时候帮助你。

For the most part (not always) array.length, System.out, and System.err are the most common 3 you'll run into that are actually member access instead of method calls.

对于大多数(并非总是)数组。长度,系统。和系统。err是最常见的3,您将遇到的实际上是成员访问,而不是方法调用。

#4


0  

int[] myArray = new int[10];
String myString = "hello world!";
List<int> myList = new ArrayList<int>();

myArray.length    //gives the length of the array
myString.length() //gives the length of the string
myList.size()     //gives the length of the list

Its very likely that strings and arrays were designed at different times and hence ended up using different conventions. One justification is that since Strings use arrays internally a method length() was used to avoid duplication of the same information. Ultimately this is just an inconsistently that evolved that would definitely be fixed if the language were ever redesigned from the ground up. :D

字符串和数组很可能是在不同的时间被设计的,因此最终使用不同的约定。一个理由是,由于字符串内部使用数组,所以使用方法length()来避免相同信息的重复。最终,这只是一种不一致的进化,如果重新设计语言的话,它肯定会被修正。:D

#5


0  

The main difference is that in the A) first case its Array Type for example int[], Object[], double[], ect.. that has a public field called lenght and the B) second case is a Object String that has a function called length(), the function could of been called getLength() or something else. The array type public field length is probably a hangover from C++ way of doing things.

主要的区别是在A)第一个例子中,它的数组类型例如int[]、对象[]、double[]、ect..它有一个公共字段叫做lenght, B)第二个例子是一个对象字符串,它有一个名为length()的函数,这个函数可以被称为getLength()或者其他东西。数组类型的公共字段长度可能是c++的遗留问题。

Array Types have the following:

数组类型如下:

  • The public final field length, which contains the number of components of the array (length may be positive or zero)
  • 公共最终字段长度,它包含数组的组件数量(长度可以是正数或零)
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions
  • 公共方法克隆,它覆盖类对象中相同名称的方法,并没有检查异常。
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method
  • 从类对象继承的所有成员;对象唯一没有继承的方法是它的克隆方法

Take a look at this, Array Types.

看看这个,数组类型。

#6


0  

.length() is a method of a String class and which returns the number of characters in the string.

.length()是一个字符串类的方法,它返回字符串中的字符数。

.length will give the number of elements stored in an array.

.length将给出存储在数组中的元素的数量。

public class length
{
public static void main(String args[])
{
String x="test";
int a[]={1,2,3,4};
System.out.println(x.length());
System.out.println(a.length);
}
}

// output
4
4

#7


0  

length is a pseudo-data member reference, and only works for (small-a) arrays (ignoring classes that you may define that implement the member).

length是一个伪数据成员引用,只适用于(小a)数组(忽略实现该成员的可能定义的类)。

Everything else is an object of a class, and all JDK classes that have this concept define either length() or size() instance methods. (I do wish they'd been consistent and picked one or the other, but that's water over the bridge.)

其他的都是类的对象,所有具有此概念的JDK类都定义length()或size()实例方法。(我真希望他们能始终如一地挑出一个或另一个,但那是天方夜谭。)

#8


0  

Array.length is a property of that Array, similar to a variable reference.

数组中。长度是该数组的属性,类似于变量引用。

ArrayList.size() is an actual method call to the array list object.

size()是对数组列表对象的实际方法调用。

#1


8  

.length;

directly accesses a field member.

直接访问字段成员。

.length();

invokes a method (i.e. an accessor) to access a field member.

调用方法(即访问器)来访问字段成员。

in the case of String, this is to be expected since it's immutable.

对于字符串,这是可以预料的,因为它是不可变的。

#2


3  

Arrays are handled differently than Strings or ArrayLists or anything else that can be counted in Java. An Array is pretty much a native type and it's length can never be changed after it is initialized, so there's no need for encapsulation. The length variable can be directly exposed with no side effects.

数组的处理方式与字符串、数组列表或其他可以在Java中计数的东西不同。数组基本上是一个本机类型,它的长度在初始化后永远不会更改,因此不需要封装。长度变量可以直接暴露,没有副作用。

The reason why String uses a method instead of a variable is because it internally uses a char[] that it doesn't want to expose publicly (for immutability/encapsulation), so it wraps the length variable in a length() method. It's the same reason ArrayList has a size() method instead of a length variable.

String之所以使用方法而不是变量,是因为它在内部使用了一个char[],而它不希望公开(为了不变性/封装),所以它将length变量封装在一个length()方法中。这也是ArrayList使用size()方法而不是长度变量的原因。

The only time you'll use the variable instead of the method is with arrays. Everything else will be methods. That is, you'll use the brackets for everything except arrays.

使用变量而不是方法的惟一时间是使用数组。其他都是方法。也就是说,除了数组之外,所有东西都要使用方括号。

#3


2  

The only true way to know when to use which one is experience. Though an IDE with autocompletion will usually help you out when you don't remember.

知道何时使用哪一个的唯一正确方法是经验。虽然带有自动完成功能的IDE通常会在你不记得的时候帮助你。

For the most part (not always) array.length, System.out, and System.err are the most common 3 you'll run into that are actually member access instead of method calls.

对于大多数(并非总是)数组。长度,系统。和系统。err是最常见的3,您将遇到的实际上是成员访问,而不是方法调用。

#4


0  

int[] myArray = new int[10];
String myString = "hello world!";
List<int> myList = new ArrayList<int>();

myArray.length    //gives the length of the array
myString.length() //gives the length of the string
myList.size()     //gives the length of the list

Its very likely that strings and arrays were designed at different times and hence ended up using different conventions. One justification is that since Strings use arrays internally a method length() was used to avoid duplication of the same information. Ultimately this is just an inconsistently that evolved that would definitely be fixed if the language were ever redesigned from the ground up. :D

字符串和数组很可能是在不同的时间被设计的,因此最终使用不同的约定。一个理由是,由于字符串内部使用数组,所以使用方法length()来避免相同信息的重复。最终,这只是一种不一致的进化,如果重新设计语言的话,它肯定会被修正。:D

#5


0  

The main difference is that in the A) first case its Array Type for example int[], Object[], double[], ect.. that has a public field called lenght and the B) second case is a Object String that has a function called length(), the function could of been called getLength() or something else. The array type public field length is probably a hangover from C++ way of doing things.

主要的区别是在A)第一个例子中,它的数组类型例如int[]、对象[]、double[]、ect..它有一个公共字段叫做lenght, B)第二个例子是一个对象字符串,它有一个名为length()的函数,这个函数可以被称为getLength()或者其他东西。数组类型的公共字段长度可能是c++的遗留问题。

Array Types have the following:

数组类型如下:

  • The public final field length, which contains the number of components of the array (length may be positive or zero)
  • 公共最终字段长度,它包含数组的组件数量(长度可以是正数或零)
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions
  • 公共方法克隆,它覆盖类对象中相同名称的方法,并没有检查异常。
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method
  • 从类对象继承的所有成员;对象唯一没有继承的方法是它的克隆方法

Take a look at this, Array Types.

看看这个,数组类型。

#6


0  

.length() is a method of a String class and which returns the number of characters in the string.

.length()是一个字符串类的方法,它返回字符串中的字符数。

.length will give the number of elements stored in an array.

.length将给出存储在数组中的元素的数量。

public class length
{
public static void main(String args[])
{
String x="test";
int a[]={1,2,3,4};
System.out.println(x.length());
System.out.println(a.length);
}
}

// output
4
4

#7


0  

length is a pseudo-data member reference, and only works for (small-a) arrays (ignoring classes that you may define that implement the member).

length是一个伪数据成员引用,只适用于(小a)数组(忽略实现该成员的可能定义的类)。

Everything else is an object of a class, and all JDK classes that have this concept define either length() or size() instance methods. (I do wish they'd been consistent and picked one or the other, but that's water over the bridge.)

其他的都是类的对象,所有具有此概念的JDK类都定义length()或size()实例方法。(我真希望他们能始终如一地挑出一个或另一个,但那是天方夜谭。)

#8


0  

Array.length is a property of that Array, similar to a variable reference.

数组中。长度是该数组的属性,类似于变量引用。

ArrayList.size() is an actual method call to the array list object.

size()是对数组列表对象的实际方法调用。