-1

Hi,I am a beginner of Java, I was taught to use "ArrayList" in OO programming in the Java lecture, however, I came across "List" today and have no idea how to use it, so what the difference between ArrayList and List? And what the same attributes of them?

something like:

           List<...>list=new List<...>()
           ArrayList<...>list=new ArrayList<...>()
pei wang
  • 295
  • 2
  • 8
  • 17

4 Answers4

4

List is an interface, whereas ArrayList is a concrete class that implements that interface

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

List is an interface.

ArrayList is a class that implements List.

You can't instantiate an interface, you have to instantiate one of classes which implements it.

M. Abbas
  • 6,409
  • 4
  • 33
  • 46
  • Thanks mabbas, does it mean I can instantiate ArrayList without creating class, for example, I can use it in main() method directly, but I cannot do the same thing with List? – pei wang Oct 08 '13 at 20:11
  • @peiwang You just need to import the package it's included in. That is `java.util`. – Sotirios Delimanolis Oct 08 '13 at 20:16
  • @peiwang I am not sure i understand your question, but to be able to use `ArrayList` in `main` method or any other method, you still need to create an instance of this class. Ex: `List<...> theList = new ArrayList<...>();` – M. Abbas Oct 08 '13 at 20:24
  • @mabbas, thanks for the reply, so, List<...>theList=new ArrayList<...>(); and ArrayList<...>theList=new ArrayList<...>();have the same functions to store the content, right? – pei wang Oct 08 '13 at 20:25
  • Yes but as @stefanoSanfilippo said, using `List` as a type for `theList` reduces the effort if someday you want to replace ArrayList with another class that implements `List`. So it is rather a good practice to refer to objects by their interfaces. I recommend you to take a look on this post http://jtechies.blogspot.fr/2012/07/item-52-refer-to-objects-by-their.html – M. Abbas Oct 08 '13 at 20:34
0

List is an interface, essentially providing a list of operations (add, remove, get...), but no implementation (you cannot do new List). There are several classes implementing List interface, including ArrayList (using, as said, an array as internal container) and, for instance, LinkedList. You can instantiate these, instead, and write:

List<ElementType> myList = new ArrayList<ElementType>();

Using List as a type for myList reduces the effort if you want to replace ArrayList with LinkedList:

List<ElementType> myList = new LinkedList<ElementType>();

(immagine, instead, if you had to replace ArrayList with LinkedList in several places, instead). Additionally, you will hide the actual implementation lying behind, so that other programmers don't make tricky assumptions on how the List might behave.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
0

List defines the basic contract that is expected that implementations would provide. ArrayList is a implementation of this contract that is backed by a dynamic array.

The great thing about this idea is, if you have a method that needs access to some kind of List, you can simply ask that callers pass you any implementation of List, meaning you don't need to know or care how the List is actually implemented, only that it will provide the contract described by List

You can't create an instance of List directly, you need to use one of the implementations, like ArrayList or LinkedList...

For example...

List<String> listOfStrings = new ArrayList<String>(25);
List<String> anotherListOfStrings = new LinkedList<String>();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366