Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 151 An Array Implementation of List
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

An Array Implementation of List

by admin last modified 2005-05-11 18:13

An Array Implementation of List

Implementing List with an array

One way to implement a list is to use an array of Objects.

class OurList implements List {
    private int nitems;
    private Object[] data;

    OurList(){
        nitems = 0;
        data = new Object[10];
    }

    OurList(int capacity){
        nitems = 0;
        data = new Object[capacity];
    }

    private void resize(){
        Object[] temp = data;
        data = new Object[2 * temp.length];
        for(int k = 0; k < temp.length; k++)
            data[k] = temp[k];
    }

    public int size(){
        return nitems;
    }

    public void clear(){
        this();
    }

    public boolean isEmpty(){
        return nitems==0;
    }

    public Object get(int index){
        if(index<0 || index>=nitems)
            throw new ArrayOutOfBoundsException();
        return data[index];
    }

    public Object set(int index, Object obj){
        if(index<0 || index>=nitems)
            throw new ArrayOutOfBoundsException();
        Object returnObject = data[index];
        data[index] = obj;
        return returnObject;
    }               

    public boolean add(Object obj){
        if(nitems >= data.length)
            resize();
        data[nitems] = obj;
        ++nitems;
        return true;
    }

    public void add ( int index, Object obj){
        if(index<0 || index>nitems)
            throw new IndexOutOfBoundsException();
        if(nitems >= data.length)
            resize();
        for(int k=nitems; k>index; k--)
            data[k] = data[k-1];
        data[index] = obj;
        ++nitems;
    }

    public Object remove (int index){
        if(index<0 || index>=nitems)
            throw new IndexOutOfBoundsException();
        Object returnObject = data[index];
        for(int i = index; i < (nitems-1); i++)
            data[i] = data[i+1];
        --nitems;
        return returnObject;
    }

    public boolean equals(Object obj){
        boolean result;
        if(getClass() != obj.getClass())
            result = false;
        else {
            OurList other = (OurList) obj;
            if(nitems != other.nitems)
                result = false;
            else {
                result = true;
                for(k=0; k<nitems; k++)
                    if(data[k] != other.data[k])
                        result = false;
                }
            }
        return result;
    }

    public Iterator iterator(){
        return new Iterator(){
            int currentIndex = 0;
            public boolean hasNext(){
                return currentIndex < nitems;
            }
            public Object next(){
                return data[currentIndex++];
            }
        };          
    }

}
           




 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: