2

I found this answer on Stack Overflow:

length is constant which is used to find out the array storing capacity not the number of elements in the array

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. 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.

size() works with collection, length works with arrays in java

(Difference between size and length methods? , 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. I know there's a method for ArrayLists. However, how can a.size be equal to one if normal arrays are used? Am I missing something?

Anonymous
  • 902
  • 10
  • 23
  • The answer there is not saying that an array has a `size` method. The user is just saying that the array's size is 1. And the user specifically mentions that "`size` works with collection" as in lists. The user is not wrong, maybe a little confusing but not wrong. – tima Sep 06 '17 at 11:07
  • @tima: I am sorry, but the answer is saying *exactly* that. I quote: *Will result in `a.size` = 1 and `a.length` = 5.* (Here, `a` is an array.) – NPE Sep 06 '17 at 11:08
  • @NPE seems to me like it was not actual code but just an expression, but I get why it can be taken in this way – tima Sep 06 '17 at 11:10
  • The authors's probably just chosen a confusing representation. – Anonymous Sep 06 '17 at 11:26
  • You should not confuse correctness and upvotes. It often happens that upvoted answers are plain wrong. We generally try to repair this ... by down-voting. – Stephen C Sep 06 '17 at 12:04

3 Answers3

4

You are correct and the answer is mistaken: Java arrays don't have a .size attribute, only .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.

An ArrayList does in fact typically have a .size member alongside the element array:

class ArrayList<T> implements ... {
    private T[] elements;
    private int size;
    ...
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    Look at the original author's answer by clicking on "edited" and studying the history. Someone (not the author) had changed the answer from "example a[0]=10 ,size=1 and length=5" to what it says now. The original author never mentioned `a.size`. Instead, he seems to have implied that `a` has a conceptual size of 1. It's the edit that got it wrong. – Klitos Kyriacou Sep 06 '17 at 11:20
  • @KlitosKyriacou: Even the "conceptual size" is misleading as it clearly depends on the application (for some, the conceptual size is five right after the "int a[] = new int[5]"). But yes, the edit didn't do the answer any favours. – NPE Sep 06 '17 at 11:23
  • indeed. I'm not sure exactly what the OP meant. – Klitos Kyriacou Sep 06 '17 at 11:24
  • @KlitosKyriacou: I was even tempted to fix the answer, but then that would invalidate this question, disadvantaging our OP here. – NPE Sep 06 '17 at 11:27
2

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. Notice no exception for the array[4]: this element exists.

The same is not true for 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));
daniu
  • 14,137
  • 4
  • 32
  • 53
  • I know. It's just the author used size as an attribute of an array which, of course, does not exist. I wanted to make sure, I'm not missing anything. – Anonymous Sep 06 '17 at 11:22
1

However, how can a.size be equal to one if normal arrays are used? 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.

The size method checks how many actually elements present in the array and returns that count.

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307