Title: Beginning ArrayList Concepts
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Beginning ArrayList Concepts

Java uses Interfaces in Data Structures

We will discuss Data Structures in more depth later in the semester. But for the time being, we will introduce the ArrayList class.

1. The ArrayList is like an array, except that the data structure automatically grows. You don't need to know the maximum size when you create it like you would for an array.
2. Although the ArrayList class has many methods, we will focus on 3 methods: size(), get(int), add(Object).
3. With ArrayLists, you can use something call "generics" to specify what you are storing in the ArrayList. We will go into this in more depth later in the semester. If you don't use the generics notation, you will be specifying an ArrayList which stores the Object class ... we will demo this with objects of type String and Person. Note the need to cast on the call to get.
4. Now use Generics to restrict what kind of object can be stored in the ArrayList.
5. Look up ArrayList in the Java java.util package documentation. Note that it implements an Interface called List. Redo your example to create a data type of List, but have the object remain an ArrayList. Note that your program runs the same.
6. There are other data structures which also implement the List interface (i.e. LinkedList). Although LinkedList and ArrayList probably don't share any code, they implement the same interface. Note that you can conveniently change your object type from ArrayList to LinkedList because they implement the same interface.
7. So one common thing is for Java to use an Interface to specify some common methods. We can have 2 totally separate hunks of code which both implement the interface. This is a very common thing for Java to do.

List interface examples: ArrayList, LinkedList


package simple_data_structures;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Person
{
    int age;
    String name;
    double height;
    Person(String n, int a, double h)
    {
        name = n;
        age = a;
        height = h;
    }
    public String toString()
    {
        return name + " " + age + " " + height;
    }
}
public class TestDataStructures {

    //Start out with ArrayList across the board.
    // Then make the datatype be List
    // Then make the class type be LinkedList

    public static void testArrayList()
    {
        ArrayList aso = new ArrayList();
        //LinkedList aso = new LinkedList();
        //List aso = new ArrayList();
        //List aso = new LinkedList();

        aso.add("red");
        aso.add("blue");
        aso.add("green");

        ArrayList<String> as = new ArrayList<String>();
        //LinkedList<String> as = new LinkedList<String>();
        //List<String> as = new ArrrayList<String>();
        //List<String> as = new LinkedList<String>();
        as.add("red");
        as.add("blue");
        as.add("green");



        for (int i=0; i < as.size(); i++)
        {
            String s1 = as.get(i);
            String s2 = (String)aso.get(i);
            System.out.println(s1 + " " + s2);
        }

        ArrayList apo = new ArrayList();
        //LinkedList apo = new LinkedList();
        // List apo = new ArrayList();
        //List apo = new LinkedList();
        apo.add(new Person("Fred", 29, 70.0));
        apo.add(new Person("George", 35, 75.0));
        apo.add(new Person("Mike", 21, 63.0));
        apo.add(new Person("Sam", 25, 73.0));
        apo.add(new Person("Ned", 15, 69.0));

        ArrayList<Person>ap = new ArrayList<Person>();
        //LinkedList<Person>ap = new LinkedList<Person>();
        //List <Person>ap = new ArrayList<Person>();
        //List<Person> ap = new LinkedList<Person>();
        ap.add(new Person("Fred", 29, 70.0));
        ap.add(new Person("George", 35, 75.0));
        ap.add(new Person("Mike", 21, 63.0));
        ap.add(new Person("Sam", 25, 73.0));
        ap.add(new Person("Ned", 15, 69.0));



        for (int i=0; i < ap.size(); i++)
        {
            Person p1 = ap.get(i);
            Person p2 = (Person)apo.get(i);
            // Note that the Person class has a toString method
            System.out.println(p1 + " " + p2);
        }


    }

    public static void main(String[] args) {

        testArrayList();



    }
}



Last Updated: January 8, 2014 0:30 AM