-2

I have a java file from a teacher and I found the following lines of code that I do not understand very well. I understand that he declares an ArrayList where each element is a List of Integers? What is List? An ArrayList? LinkedList? Thank you!

ArrayList<List<Integer>>L=new ArrayList<List<Integer>>();
ArrayList<Integer>l=new ArrayList<Integer>();
Stefan
  • 83
  • 1
  • 6
  • 20
  • 1
    `List` is an interface. It can be any class that implements it, including `ArrayList`, `LinkedList`, or something else. – Colonel Thirty Two Oct 22 '14 at 14:04
  • 1
    possible duplicate of [Type List vs type ArrayList in Java](http://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java) –  Oct 22 '14 at 14:04

3 Answers3

0

A turn through the javadocs would be a good thing to do, perhaps more valuable than this answer.

If you're going to write Java, you need to know how to research the javadocs.

Find the java.util package. You'll see List and ArrayList defined there. The former is an interface, the latter a concrete class that implements the List interface using a backing array as its implementation.

I would suggest that you and your professor should prefer this:

List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();
List<Integer> l = new ArrayList<Integer>();
listOfLists.add(l);

The reason is that preferring the interface type allows you to substitute in other implementations without affecting client code.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

A List is an interface class for ArrayList and LinkedList and any other type of list that implements List. ArrayList is a generic list that uses an array to store objects. A LinkedList is a generic list that uses Nodes to store objects. Each type of list must implement certain methods from the List interface. Each type of list has it's advantages and disadvantages. You may want to look up the difference between LinkedLists and ArrayLists. Here is a link that might help you.

Community
  • 1
  • 1
brso05
  • 13,142
  • 2
  • 21
  • 40
0

Before looking at the code, you have to understand the difference between a Interface and an Implementation. From Wikipedia:

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final). All methods of an Interface do not contain implementation (method bodies) as of all versions below JAVA 8. Starting with Java 8 default and static methods may have implementation in the interface definition.1

Once you understand the difference between the implementation and the interface, the two declarations become easier to understand. Let's start with the second statement first - it is more straightforward:

ArrayList<Integer>l=new ArrayList<Integer>();

Here you are declaring l as being an ArrayList that accepts Integer objects. So an eventual use would be something like:

l.add( new Integer(1) );

If you follow that logic then you can see that the other line is the same construct:

ArrayList<List<Integer>>L=new ArrayList<List<Integer>>();

Here you are defining L to be an ArrayList that accepts List<Integer> objects. A List is an interface in Java - there can be different types of implementations. Ex: LinkedList, ArrayList, etc. The actual implementation is irrelevant in the declaration. So given that an ArrayList is actually a List, you can see that L can contain instances of l since l is a List<Integer>.

So an eventual use of the second list would be:

L.add(l);

In fact, if you wanted to take it even further, you can do:

List<Integer> linkedList = new LinkedList<Integer>();
List<Integer> vector = new Vector<Integer>();

And add them all to the same L list since they all implement the List<Integer> interface:

L.add(linkedList);
L.add(vector);
Eric B.
  • 23,425
  • 50
  • 169
  • 316