Lecture 3b: Containers: Vector and Arraylist


I. Containers

The containers available to us are in the Collection object from the java.util package. It inherits out some stuff for networks (bean bags), some stuff for sets and some stuff for lists.

So what's the difference between Set and List? A Set can only have unique elements in that no two elements in a Set can be the same. A List is just a sequence of elements with no requirements at all.

We are concerned with the Lists, all of them... but LinkedList will be covered later.

II. Vector and ArrayList

What's the difference between these two? Well, at this point both are about the same. They are different when you get into concurrent and parallel programming with threading. But for now, they are essentially the same with ArrayList performing faster than Vector. Vector allows for threading without special considerations... but let's not go there.

Life after Java v. 1.5

After Java 1.5 Vector and ArrayList are more like vector in C++, you can cast it to a type with Vector<Object> or ArrayList<Object> now... but that's just adding more overhead. You can use Vector and ArrayList like that... but you can only do it to class inherited from Object, so you can't do Vector<double>, but you can do Vector<String>.

Collections and Inheritance

This is more powerful but slightly annoy. You can have a Vector or ArrayList of elements of an interface or abstract class... or any parent class and add new objects instantiated as the child class in the collections.

For example:

We have our class A and B again:

public class A {
    public A() {
        System.out.println("Constructing A");
    }
   
    public void print() {
        System.out.println("Executing print in A");
    }
   
    public void print2() {
        System.out.println("This is the function that doesn't do anything");
    }
}


public class B extends A{
    public B(){
        System.out.println("Constructing B");
    }
    public void print() {
        System.out.println("Executing print in B");
        super.print();
    }
    public void print2(){
        System.out.println("Executing print2 in B");
    }
}


import java.util.*;

public class Main
{
    private static ArrayList<A> stuff;
    public static void main ( String[] args )
    {
        stuff = new ArrayList<A>();
       
        stuff.add(new A());
        stuff.add(new B());
        stuff.add(new B());
        stuff.add(new B());
       
        for (int i = 0; i<stuff.size(); i++)
        {
            stuff.get(i).print();
            stuff.get(i).print2();
        }
    }
}

Output:

Constructing A
Constructing A
Constructing B
Constructing A
Constructing B
Constructing A
Constructing B
Executing print in A
This is the function that doesn't do anything
Executing print in B
Executing print in A
Executing print2 in B
Executing print in B
Executing print in A
Executing print2 in B
Executing print in B
Executing print in A
Executing print2 in B

So as we can see, inheritance adds power to the collection objects. But do note that if there is a special function declared only in class B... you can't access that function unless you cast that element to a type of class B.

Class exercise

Design your project 1.

Video Apples to Apples entries

Green Apple: Politician/Political

Entries: