Java List and ArrayList
Hi guys,
is there any difference between these two ways?
List wordlist = new ArrayList <> ();
and
ArrayList wordlist = new ArrayList ();
Difference
List
is abstract, so you can change it's 'type'. ArrayList
is not, but it will allow you more speed if you know all your types are going to be the same.
Which one to use?
I have always used ArrayList
, and have never found any need for List
.
https://java2blog.com/java-list-vs-arraylist/
Solution
the list in java is not dynamic, Meaning it can hold only a certain amount on information, based on what you put. So whatever value you want it to hold, that's all it can. While the array list is a dynamic. You don't have to specify anything. You can add how much you want. the internal process just makes more, and more space foryou to add unlimited.
no, you're wrong, arrays have stsatic length, but list and arraylist have dynamic length
@RYANTADIPARTHI
The main differences between List and ArrayList are given below List is an interface while ArrayList is a class.
List wordlist = new ArrayList <> ();
In the above line, we are assigning the ArrayList class object to the List interface.
There is a big plus on declaring them using the interface, which is what is known as "coding to an interface" instead of "coding to an implementation" which is a big Object-Oriented Design (OOD) principle.
Declaring them using an interface, means you can later substitute that value with any other concrete class that implements that interface, instead of being locked into that particular concrete class
You can take full advantage of polymorphism by declaring them using an interface, because each implementation can call the correct method at runtime.
You follow the OOD principle of code to an interface.
https://javahungry.blogspot.com/2017/11/arraylist-in-java-tutorial-with-example.html
Hope this will clear yours doubt.