1

I am newly learning java.I am trying to understand the various data structures in util package.

/*create an ArrayList object*/
List<String> arrayList = new ArrayList<String>();

In the decleration of ArrayList class I have seen

public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

My question is if I use an object of type List<> to store the object returned by constructor of ArrayList<>

will it work.You may assume that I need to use only the methods specified in the List interface,such as add() or size().

I have tried this on my machine and it does not work.I found this in the java tutorial below

http://www.javamex.com/tutorials/collections/using_1.shtml

The complete example code that I tried is as below.

package arraylist;
import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {

        public static void main(String[] args)
        {

        /*create an ArrayList object*/
         List<String> arrayList = new ArrayList<String>();

                /*
                Add elements to Arraylist using
                boolean add(Object o) method. It returns true as a general behavior
                of Collection.add method. The specified object is appended at the end
                of the ArrayList.
                 */
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");

        /*
        Use get method of Java ArrayList class to display elements of ArrayList.
        Object get(int index) returns and element at the specified index in
        the ArrayList
         */
        System.out.println("Getting elements of ArrayList");
        System.out.println(arrayList.get(0));
        System.out.println(arrayList.get(1));
        System.out.println(arrayList.get(2));
    }
}

I forgot to import List.that is why it did not work.However my next question is we use a variable of type List to store on object of type ArrayList.

Comparing the methods of List and ArrayList ArrayList has a method called trimToSize() that is not there in list.

In the above case will I be able to call that method?

If yes,what that generally means is that a base class pointer can be used to store a derived class object as the method list of derived class will always be a super-set of base class methods?

Is my above conclusion correct? It my sound stupid question but I am very new to java.

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 2
    It should work, what exactly did you write and what is the error message? – A--C Jan 24 '13 at 01:08
  • 2
    You need to provide actual code in your question if we are to have any chance of helping you. What was the code you wrote? What was the error you saw? "it does not work" is not sufficiently descriptive. – Recurse Jan 24 '13 at 01:11
  • 4
    You also have to import `java.util.List`. – Paul Bellora Jan 24 '13 at 01:12

4 Answers4

3

Since ArrayList implements the List interface you should be able to use the object declared as a List that is initialized to an ArrayList. This is pretty common practice. It allows you to switch the implementation of the list without code changes.

Make sure your importing the list interface:

package arraylist;
import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {

        public static void main(String[] args)
        {

        /*create an ArrayList object*/
         List<String> arrayList = new ArrayList<String>();

                /*
                Add elements to Arraylist using
                boolean add(Object o) method. It returns true as a general behavior
                of Collection.add method. The specified object is appended at the end
                of the ArrayList.
                 */
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");

        /*
        Use get method of Java ArrayList class to display elements of ArrayList.
        Object get(int index) returns and element at the specified index in
        the ArrayList
         */
        System.out.println("Getting elements of ArrayList");
        System.out.println(arrayList.get(0));
        System.out.println(arrayList.get(1));
        System.out.println(arrayList.get(2));
    }
}

When referencing the List you will not be able to use the trimToSize function since it is not on the interface for List

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • @liv2hak I answered the question about `trimToSize` – Kevin Bowersox Jan 24 '13 at 01:30
  • It allows you to switch the implementation of the list without code changes.? Can you please explain what this statement means in a bit more detail? – liv2hak Jan 24 '13 at 01:31
  • 1
    It means if you only use the methods on the `List` interface. You can then change the initialization of your list another class that implements the list interface such as `LinkedList`. `List arrayList = new LinkedList();` – Kevin Bowersox Jan 24 '13 at 01:32
2

Consider this example,

class A implements B {  }

In this case A class may be assigned to a variable of the type of one of its implemented interfaces.

C c = new A();

If you do this casting when you use instance of c, only the methods available in Class C will be accessible.

Similarly ArrayList is your ClassA and your List is Class B in example.

In the below using arraylist instance you can invoke methods available in List interface,

/*create an ArrayList object*/
List<String> arrayList = new ArrayList<String>();

And If you use the above assignment you cannot call ArrayList methods like trimToSize() etc. If you want to use ArrayList methods just use as below.

/*create an ArrayList object*/
ArrayList<String> arrayList = new ArrayList<String>();

Most of the developers, 90% of the time use the methods provided by List and thats the reason all the example out there is following the former method.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
1

List<String> arrayList means 'this variable can only store implements of the List<String> interface'

ArrayList<String> implements List<String> so it can live there.

Patashu
  • 21,443
  • 3
  • 45
  • 53
1

Edit the top of the file to look like this:

package arraylist; import java.util.ArrayList; import java.util.List;

....

If you haven't imported List, you can't use it as the type for a variable. This is the problem you are experiencing.

Recurse
  • 3,557
  • 1
  • 23
  • 36