Jsou zobrazeny jen nové odpovědi. Zobrazit všechny
Předmět Autor Datum
No, uzavřel bych to s tím, že mě jenom zajímalo, jestli je to možné, ale lze to nastavit pouze u kla…
MichalDM 22.06.2019 23:42
MichalDM
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elem…
vlada01 23.06.2019 00:00
vlada01
OK. Díky poslední
MichalDM 23.06.2019 00:06
MichalDM

No, uzavřel bych to s tím, že mě jenom zajímalo, jestli je to možné, ale lze to nastavit pouze u klasického pole.

V podstatě, u pole je to nutnost. U ArrayListu je pouze možné zadat, s jakým počtem prvků se inicializuje. (normálně je to 0)

I když, přesto bych se chtěl zeptat. Jaký je rozdíl mezi

var without_capacity = new ArrayList<Object>();

a

var with_capacity = new ArrayList<Object>(100);

Pokud následně přidáš 100 objektů, tak je výsledek stejný.

K čemu je teda inicializace dobrá?

Díky

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Jedná se o prvotní velikost pole, ve kterém se elementy ukládají.

Zpět do poradny Odpovědět na původní otázku Nahoru