我怎样才能得到一个数组的大小,集合或Java中的字符串? [英] How can I get the size of an array, a Collection, or a String in Java?

查看:214
本文介绍了我怎样才能得到一个数组的大小,集合或Java中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是我可以访问一个数组的长度不同的方式,集合(列表设置等),和一个字符串对象吗?它为什么不一样?

What are the different ways that I can access the length of an array, a collection (List, Set, etc.), and a String object? Why is it different?

推荐答案

对于数组:使用 。长度

Abridged:

For an array: use .length.

对于收藏(或地图)使用 .size( )

For a Collection (or Map): use .size().

对于的CharSequence (其中包括的CharBuffer 字符串的StringBuffer 的StringBuilder ):使用 。长度()

For a CharSequence (which includes CharBuffer, Segment, String, StringBuffer, and StringBuilder): use .length().

有人会使用一个数组的 。长度属性即可访问它。尽管阵列是一个动态创建的对象,为的任务长度属性由的Java语言规范,10.3节

One would use the .length property on an array to access it. Despite an array being a dynamically created Object, the mandate for the length property is defined by the Java Language Specification, §10.3:

这是数组的数组,创作前pression的(§15.10)
  数组初始化(第10.6节)

An array is created by an array creation expression (§15.10) or an array initializer (§10.6).

数组创建前pression指定的元素类型,数量
  嵌套阵列的水平,并且阵列的对的至少一个的长度
  的嵌套级别。 数组的长度,可作为最后
  实例变量长度

An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable length.

数组初始化创建一个数组,并提供初始值
  它的所有组件。

An array initializer creates an array and provides initial values for all its components.

由于数组的长度不能更改,恕不创建一个新的数组实例,。长度的重复访问的不会更改值不管什么做到阵列实例(除非其引用被替换为不同尺寸的阵列)。

Since the length of an array cannot change without the creation of a new array instance, repeated accesses of .length will not change the value, regardless of what is done to the array instance (unless its reference is replaced with a differently sized array).

作为一个例子,得到一个声明一维数组的长度,一会这样写:

As an example, to get the length of a declared one-dimensional array, one would write this:

double[] testScores = new double[] {100.0, 97.3, 88.3, 79.9};
System.out.println(testScores.length); // prints 4

要获得长度在 N 的维数组,需要记住,他们是在一次访问数组的一个方面来承担。

To get lengths in an n-dimensional array, one needs to bear in mind that they are accessing one dimension of the array at a time.

下面是一个为二维阵列的实例。

Here's an example for a two-dimensional array.

int[][] matrix
      = new int[][] {
                         {1, 2, 3, 4},
                         {-1, 2, -3, 4},
                         {1, -2, 3, -4}
    };

System.out.println(matrix.length); // prints 3 (row length or the length of the array that holds the other arrays)
System.out.println(matrix[0].length); // prints 4 (column length or the length of the array at the index 0)

这是很重要的利用,尤其是在交错数组的情况;列或行可能不会总是排队的所有时间。

This is important to make use of, especially in the case of jagged arrays; the columns or rows may not always line up all the time.

对于实现收藏界面中的每个对象,他们将有一个方式名为<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html#size%28%29\"><$c$c>size()与访问集合的整体大小。

For every object that implements the Collection interface, they will have a method called size() with which to access the overall size of the collection.

与数组不同,藏品的不固定长度,然后可以添加或删除在任何时候的元素。到 A调用大小()将产生一个非零结果当且仅当出现了任何添加到列表本身。

Unlike arrays, collections are not fixed length, and can have elements added or removed at any time. A call to size() will produce a nonzero result if and only if there has been anything added to the list itself.

例如:

List<String> shoppingList = new ArrayList<>();
shoppingList.add("Eggs");
System.out.println(shoppingList.size()); // prints 1

某些集合可拒绝添加元素,可能是因为它的,或者它是一个重复的(在设置的情况下)。在这种情况下,反复添加到集合不会导致大小递增

Certain collections may refuse to add an element, either because it's null, or it's a duplicate (in the case of a Set). In this case, repeated additions to the collection will not cause the size to increment.

例如:

Set<String> uniqueShoppingList = new HashSet<>();
uniqueShoppingList.add("Milk");
System.out.println(uniqueShoppingList.size()); // prints 1
uniqueShoppingList.add("Milk");
System.out.println(uniqueShoppingList.size()); // prints 1

访问名单,LT的大小;列表&LT;对象&gt;&GT; *以类似的方式进行,以交错数组:

Accessing the size of a List<List<Object>>* is done in a similar way to a jagged array:

List<List<Integer>> oddCollection = new ArrayList<>();
List<Integer> numbers = new ArrayList<Integer>() {{
    add(1);
    add(2);
    add(3);
}};
oddCollection.add(numbers);
System.out.println(oddCollection.size()); // prints 1
System.out.println(oddCollection.get(0).size()); // prints 3

*:收藏没有在它的接口中定义的 GET

*: Collection doesn't have the get method defined in its interface.

顺便说一句,一个地图不是收藏,但它也有一个<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#size%28%29\"><$c$c>size()定义的方法。这只是返回包含在键 - 值对的数量地图

As an aside, a Map is not a Collection, but it also has a size() method defined. This simply returns the number of key-value pairs contained in the Map.

A 字符串有一个方式<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length%28%29\"><$c$c>length()定义。它所做的是打印的字符数present在该实例的字符串

A String has a method length() defined. What it does is print the number of characters present in that instance of the String.

例如:

System.out.println("alphabet".length()); // prints 8

这篇关于我怎样才能得到一个数组的大小,集合或Java中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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