Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size has private access in ArrayList

I wrote a pretty standard bit of code utilizing String populated ArrayList but when I try running it, I get the following error:

error: size has private access in ArrayList.

The code is as follows:

System.out.println(testedArticles.size);
like image 529
eggHunter Avatar asked Oct 18 '13 13:10

eggHunter


People also ask

Does ArrayList have a size limit?

The theoretical limit for ArrayList capacity is Integer. MAX_VALUE, a.k.a. 2^31 - 1, a.k.a. 2,147,483,647. But you'll probably get an OutOfMemoryError long before that time because, well, you run out of memory.

What is the size of an ArrayList?

When you create an object of ArrayList in Java without specifying a capacity, it is created with a default capacity which is 10. Since ArrayList is a growable array, it automatically resizes when the size (number of elements in the array list) grows beyond a threshold.


1 Answers

You are attempting to access a private member of ArrayList, part of its internal working that are not supposed to be used externally

If you want to get the size of the arraylist you want the method:

arraylist.size()

Why is it like this

This gives the ArrayList class the option to store size in whatever way it wants. Does it just return size, probably, but it could do a number of other things instead. For example it could calculate size lazily, in which it is only calculated if someone asked for it then it stores that value until it becomes invalid (as more objects are added). This would be useful if calculating size was expensive (very unlikely to be the case here), changed often and was called only occasionally.

like image 95
Richard Tingle Avatar answered Oct 20 '22 05:10

Richard Tingle