如何在 Java 中获取数组、集合或字符串的大小? [英] How can I get the size of an array, a Collection, or a String in Java?

查看:22
本文介绍了如何在 Java 中获取数组、集合或字符串的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问数组、集合(ListSet 等)和 String<的长度有哪些不同的方法?/代码>对象?为什么不一样?

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?

推荐答案

删节:

对于数组:使用.length.

对于Collection(或Map):使用.size().

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

对于CharSequence(包括CharBufferSegmentStringStringBuffer> 和 StringBuilder):使用 .length().

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

可以使用数组上的 .length 属性 来访问它.尽管数组是动态创建的Objectlength 属性的要求由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:

数组由数组创建表达式创建 (第 15.10 节) 或数组初始值设定项 (§10.6).

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

一个数组创建表达式指定元素类型,数量嵌套数组的级别,以及至少一个数组的长度嵌套的级别.数组的长度可作为最终实例变量length.

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.

由于不创建新的数组实例就不能改变数组的长度,所以重复访问.length不会改变值,无论做什么到数组实例(除非它的引用被替换为不同大小的数组).

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.

对于实现Collection 接口的每个对象,它们都有一个方法,称为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.

与数组不同,集合不是固定长度,并且可以随时添加或删除元素.调用 size() 将产生非零结果,当且仅当列表本身添加了任何内容.

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

某些集合可能拒绝添加元素,因为它是null,或者它是重复的(在Set 的情况下).在这种情况下,重复添加集合不会导致大小增加.

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

访问 List>* 的大小的方式与锯齿状数组类似:

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

*:Collection 没有在其接口中定义 get 方法.

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

顺便说一句,Map 不是 Collection,但它还有一个 size() 方法定义.这只是返回 Map 中包含的键值对的数量.

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.

String 有一个 方法 length() 定义.它的作用是打印 String 实例中存在的字符数.

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天全站免登陆