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);
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.
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.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With