Good day, ladies and gents! Today I want to tell about one very little but important thing. Newbies will be intrested. Experts move on, please. The subject is - ArrayList capacity.
What's wrong with it? Nothing, but I want to ask you, what will happen with this test ?
What's wrong with it? Nothing, but I want to ask you, what will happen with this test ?
@Test public void listLengthTest() { List<Object> list = new ArrayList<>(10); assertThat(list.size(), is(10)); }
Answer just for yourself, not for me.
And the correct answer is - it will fail!
Why? I set initial capacity! Yes I do, but the size is a private field of ArrayList class and it reflects, how much real objects added in list and not how much memory in terms of underlying array's cells is allocated (sorry for my english if I made mistake in a sentence). size changing only when you add or remove elements.
It's not secret, it's known stuff for everyone who saw the sources of ArrayList. But if my note will help you to take a job, I'll be very glad.
Thanks for reading!
If you want to use this text, please make a link to my blog. Be polite =)
Комментарии
Отправить комментарий