tags:

views:

139

answers:

4

I am trying to implement a tree using an array and I have all my code but I am receiving the following error, any ideas how to fix this? Also if you can find any other way to implement this without all the casting I'm doing that would be great.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at assign2Trees.ArrayTreeNode.<init>(ArrayTreeNode.java:17)
    at assign2Trees.ArrayTree.<init>(ArrayTree.java:21)
    at assign2Trees.ArrayTree.main(ArrayTree.java:110)


import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class ArrayTree<E> implements Tree<E>{

    protected ArrayTreeNode<E>[] tree;
    protected E element;
    public static final int CAPACITY = 1024;
    protected List<ArrayTreeNode<E>> children;
    protected ArrayTreeNode<E> parent;
    protected ArrayTreeNode<E> root;


public ArrayTree(E e)
{
    tree = new ArrayTreeNode[CAPACITY];
    for(int i = 0; i<CAPACITY; i++)
        tree[i] = new ArrayTreeNode<E>();   
    root = tree[0];
    root.setChildren(this.children);
    root.setElement(e);
    root.setParent(null);
}
public ArrayTree(E e, int size)
{ 
    tree = new ArrayTreeNode[size];
    for(int i = 0; i<size; i++)
        tree[i] = new ArrayTreeNode<E>();
    root = tree[0];
    root.setElement(e);
    root.setChildren(this.children);
    root.setParent(null);

}
    public void delete(E node) throws RootNonDeletableException 
    {
        tree[Arrays.asList(tree).indexOf(node)] = null;
        parent(tree[Arrays.asList(tree).indexOf(node)]).addChild(tree[Arrays.asList(tree).indexOf(node)]); 
    }
    public boolean isEmpty() 
    {
        if (tree[0] == null)
            return true;
        else
            return false;
    }
    public Iterator<E> iterator() 
    {
        return new Iterator<E>()
        {
            private int index = 0;

            public boolean hasNext()
            {
                return index < ArrayTree.this.size();
            }
            public E next() throws NoSuchElementException
            {
               if (!hasNext()) 
               {
                   return null;
               }
               return (E) ArrayTree.this.tree[index++];
            }
            public void remove() 
            {

            }   
        };
    }

    public E replace(E oldE, E newE) 
    {
        tree[Arrays.asList(tree).indexOf(oldE)] = (ArrayTreeNode<E>) newE; 
        return newE;
    }

    public E root() throws EmptyTreeException 
    {
        if (isEmpty())
            throw new EmptyTreeException();
        else 
            return (E) tree[0];
    }

    public int size() 
    {
        int count = 0;
        for (int i = 0; i < tree.length; i++)
            count = count+1;
            return count;
    }   
    public Iterable<E> children(E node) 
    {
        return (Iterable<E>) children;
    }
    public ArrayTreeNode<E> parent(ArrayTreeNode<E> node)
    {
        return parent;
    }
    public E parent(E node) 
    {
        return (E) parent;
    }
    public static void main(String[] args)
    {
        ArrayTree<String> tree = new ArrayTree<String>("Eric"); 
        System.out.println(tree.size());
        tree.isEmpty();

    }

}





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

        public class ArrayTreeNode<E> {

            protected E element;
            protected List<ArrayTreeNode<E>> children;
            protected ArrayTreeNode<E> parent;

            public ArrayTreeNode()
            {
                children = new ArrayList<ArrayTreeNode<E>>();
                element = null;
                parent = children.get(0);
            }
            public ArrayTreeNode(E e)
            {
                children = new ArrayList<ArrayTreeNode<E>>();
                parent = children.get(0);
                element = e;
                this.setElement(e);
            }
            public void setElement(E e)
            {
                this.element = e;
            }
            public void setChildren(List<ArrayTreeNode<E>> kids)
            {
                this.children = kids;
            }
            public void addChild(ArrayTreeNode<E> child)
            {
                children.add(child);
            }
            public void deleteChildren()
            {
                this.children = new ArrayList<ArrayTreeNode<E>>();
            }
            public List<ArrayTreeNode<E>> getChildren()
            {
                return this.children;
            }
            public void setParent(ArrayTreeNode<E> e)
            {
                parent = e;
            }
        }
+3  A: 

You cannot cast an Object[] array to another array. See: http://forums.sun.com/thread.jspa?threadID=715376

So you must change lines like:

 tree = (ArrayTreeNode<E>[]) new Object[CAPACITY];

to

 tree = new ArrayTreeNode<E>[CAPACITY];
Plaudit Design - Web Design
A: 

You're instantiating an Object array instead of an ArrayTreeNode. Try this instead:

tree = new ArrayTreeNode<E>[CAPACITY];
MikeG
now I have a nullpointer exception: Exception in thread "main" java.lang.NullPointerException at assign2Trees.ArrayTree.<init>(ArrayTree.java:21) at assign2Trees.ArrayTree.main(ArrayTree.java:106)
user258875
@MikeG, That will give you compiler warnings, don't forget the ParametrizedType.
The Elite Gentleman
if I change it to tree = new ArrayTreeNode<E>[CAPACITY]; I get a compile error "Cannot create a generic array of ArrayTreeNode<E>[CAPACITY];
user258875
A: 

You're trying to cast an Object array to a ArrayTreeNode in this statement:

 tree = (ArrayTreeNode<E>[]) new Object[CAPACITY];

Rather, do this instead:

 tree = new ArrayTreeNode<E>[CAPACITY];

PS: [Ljava.lang.Object means that It's a 1 dimensional array [ of type (L) java.lang.Object.

The Elite Gentleman
+2  A: 

In your ArrayTree constructors, you are creating an array, and then accessing element at index[0] which is null, this is why you get a NullPointerException.

Creating an array is not the same as creating the objects held by the array. You will need to create these objects before using them.

Also, as pointed out by others, you'll need to create an array of ArrayTreeNode elements, rather than an array of Objects.

In your ArrayTreeNode you have a similar problem: You create an ArrayList which is initially empty, then try to access element at index zero (children.get(0)), which does not exist.

Grodriguez
ok so how would I do that? Something like for(int i = 0; i<CAPACITY; i++) tree[i] = new ArrayTreeNode();
user258875
are the constructors ok now? because I still get an error: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at assign2Trees.ArrayTreeNode.<init>(ArrayTreeNode.java:17) at assign2Trees.ArrayTree.<init>(ArrayTree.java:21) at assign2Trees.ArrayTree.main(ArrayTree.java:110)
user258875
Now the constructor of your `ArrayTreeNode` class has a similar problem, you are creating an `ArrayList` (which is initially empty), then accessing element at index 0 (`children.get(0)`) which does not exist. May I suggest that you break this into smaller problems and try to solve one at a time (possibly opening a new SO question for each specific problem that you need help with)?
Grodriguez
Why would that not work since children is an arrayList and it has an initial size of 10 so it should be ok.
user258875
ok so I added for(int i = 0; i < 10; i++) { children.add(i, new ArrayTreeNode<E>()); } to the constructor in ArrayTreeNode
user258875