Array with unknown size

I have a small question on how to create arrays with unknown size. I think the best way is to use java.util.ArrayList; my question is: what is the difference between:

ArrayList client_Catalog = new ArrayList<>();

and

List<> list = new ArrayList<>();

Btw in my program I am trying to create an empty list where I am going to store information about clients, therefore, objects of this class, I do not know the final size.

+4
source share
3 answers

There is no such thing as an array with an unknown length. You either have a fixed-length array, or Listone that is flexible.


, String[] , , . , .

List Interface. ArrayList .

ArrayList client_Catalog = new ArrayList<>();

: client_Catalog ArrayList . , :

List<> list = new ArrayList<>();

, List - a List. ArrayList, LinkedList , . , , , List.

. List generic. - , List . , . <>.

, ArrayList String, :

ArrayList<String> myList = new ArrayList<>();

Q & A, , . , . -, , , .

+2

. List . , .. .

0

WITH

ArrayList client_Catalog = new ArrayList<>();

and

List<> list = new ArrayList<>();

There will be no difference. Both will work and work exactly the way you are used to. The only difference is that in the second examples it cannot be only ArrayList. But also, for example, LinkedListor another object that implements an interface list .

In your case, I would choose the top one to make sure that it is always ArrayListif you want to rely on it 100%.

-1
source

Source: https://habr.com/ru/post/1660854/


All Articles