简体   繁体   English

Java数组:属性大小?

[英]Java array: Attribute size?

I found this answer on Stack Overflow: 我在Stack Overflow上找到了这个答案:

length is constant which is used to find out the array storing capacity not the number of elements in the array length是常量,用于找出数组存储容量而不是数组中元素的数量

Example: 例:

 int a[] = new int[5] 

a.length always returns 5 which is called the capacity of an array, so length always returns the CAPACITY. a.length总是返回5,称为数组的容量,因此length总是返回CAPACITY。 but

"number of elements in the array is called size" “数组中的元素数称为大小”

Example: 例:

 int a[] = new int[5] a[0] = 10 

Will result in a.size = 1 and a.length = 5 . 将导致a.size = 1a.size = 1 a.length = 5

size() works with collection, length works with arrays in java size()适用于集合, length适用于java中的数组

( Difference between size and length methods? , 2017-09-06) 尺寸和长度方法的区别? ,2017-09-06)

As this answer received five upvotes I thought it is correct. 由于这个答案得到了五个赞成票,我认为这是正确的。 But there is actually no size attribute for arrays in Java. 但实际上Java中的数组没有大小属性​​。 I know there's a method for ArrayLists. 我知道有一个ArrayLists的方法。 However, how can a.size be equal to one if normal arrays are used? 但是,如果使用普通数组, a.size如何等于1? Am I missing something? 我错过了什么吗?

You are correct and the answer is mistaken: Java arrays don't have a .size attribute, only .length . 你是对的,答案是错误的:Java数组没有.size属性,只有.length

To give the answer's author the benefit of the doubt, I suspect they are trying to explain how an array gets used internally by an ArrayList . 为了给答案的作者带来怀疑的好处,我怀疑他们正在试图解释ArrayList如何在内部使用ArrayList

An ArrayList does in fact typically have a .size member alongside the element array : 事实上, ArrayList通常在元素数组旁边有一个.size成员:

class ArrayList<T> implements ... {
    private T[] elements;
    private int size;
    ...
}

Arrays are initialized upon declaration, hence their size is always equal to their length. 数组在声明时初始化,因此它们的大小总是等于它们的长度。

int[] array = new int[5];
for (int i : array) System.out.println(i);
System.out.println(array[4]);

will print 5 times "0" for the loop, then "0" again. 将为循环打印5次“0”,然后再次打印“0”。 Notice no exception for the array[4] : this element exists. 注意array[4]没有异常:这个元素存在。

The same is not true for ArrayLists. ArrayLists也不是这样。

ArrayList<Integer> list = new ArrayList<>(5);
// no output, because no elements are contained
for (int i : list) System.out.println(i);
// Exception: no such element
System.out.println(list.get(4));

However, how can a.size be equal to one if normal arrays are used? 但是,如果使用普通数组,a.size如何等于1? Am I missing something? 我错过了什么吗?

ArrayList internally uses array and it keep growing the length of the array dynamically as you keep adding elements to it. ArrayList内部使用数组,并且在不断向其中添加元素时,它会动态增加数组的长度。

The size method checks how many actually elements present in the array and returns that count. size方法检查数组中存在的实际元素数量并返回该计数。

If you look at the source code of array list it maintains a size variable and increments it when you add an elements 如果查看数组列表的源代码,它会维护一个大小变量,并在添加元素时递增它

public void add(int index, E element) {
393         rangeCheckForAdd(index);
394 
395         ensureCapacity(size+1);  // Increments modCount!!
396         System.arraycopy(elementData, index, elementData, index + 1,
397                          size - index);
398         elementData[index] = element;
399         **size++;**
400     }

However there is no such wrapper on array and you are directly operating on arrays, hence the difference. 但是阵列上没有这样的包装器,你直接在阵列上运行,因此不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM