你如何计算java中数组的元素 [英] How do you count the elements of an array in java
本文介绍了你如何计算java中数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有数组
int theArray = new int[20];
数组的长度为20,但计数为0.我如何计算?
The length of the array is 20 but the count is 0. How can i get the count?
推荐答案
伯爵是什么意思?具有非零值的元素数量?你只需要计算它们。
What do you mean by "the count"? The number of elements with a non-zero value? You'd just have to count them.
该数组与显式之间存在无区别设置为零值。例如,这些数组是难以区分的:
There's no distinction between that array and one which has explicitly been set with zero values. For example, these arrays are indistinguishable:
int[] x = { 0, 0, 0 };
int[] y = new int[3];
Java中的数组总是有固定大小 - 通过长度访问
字段。没有当前使用的阵列数量的概念。
Arrays in Java always have a fixed size - accessed via the length
field. There's no concept of "the amount of the array currently in use".
这篇关于你如何计算java中数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文