-3

So I am trying to do this assignment on ArrayList for the first time and am stuck with a error message that I am not sure how to deal with. So first I build a class called ArrayList: some of the code for that is as follow:-

public class ArrayList
{
    ArrayList list = new ArrayList();

    public void add(Object a, int index)
    {
        list.add(null,0);
        list.add(a,index);
    }

    public void insert(Object b, int index)
    {
        list.add(b,index);
    }

    public Object remove(int index)
    {
        Object c = get(index);
        list.remove(index);
        return c;
    }

    public Object get(int index)
    {
        return (list.get(index));
    }

    public int size()
    {
        int length = list.size();
        return length;
    }
    }

there is more code to it but i think for my question this much will suffice. So after that I built another class that contains my main driver for testing which is:-

public class DataStructuresDriver
{
    public static void main(String[] args) 
    {
        //stackTests();
        //queueTests();
        arrayListTests();
    }

    private static void arrayListTests() 
    {

        //todo: make more tests here
        ArrayList a = new ArrayList();
        a.add('A',2);
        a.insert('B', 0);
        a.insert('a',0);
        a.insert('t',1);

        System.out.println(a.toString());

        while(a.isEmpty() == false) 
        {
            System.out.println(a.remove(0));
        }

    }

when i compile the two classes i get no errors but when i run my main code it gives me java.lang.StackOverflowError null, so not sure how to proceed?

Neha
  • 1
  • 1
    The title should reflect your problem (ArrayList (beginner)) is not a problem; something like "Getting StackOVerflow error when trying to add x to ArrayList is"). Also, we'd need the stack trace and the full text of the error you get for this question to be useful to others. – George Stocker Jan 29 '15 at 15:59
  • 1
    Name clash - Try changing the outer-class name or instead of "ArrayList list = new ArrayList()" try "java.util.ArrayList list = new java.util.ArrayList()" – BretC Jan 29 '15 at 16:00
  • @Bret indeed. but maybe OP should clean the question first... also, I'm not sure if OP actually wants to use java.util.ArrayList or just has some twisted idea on how things should work. – eis Jan 29 '15 at 16:00
  • If your assignement is to study Java's ArrayList, you don't even need your ArrayList class. Just remove it and import java.util.ArrayList. ;) – DeadlyJesus Jan 29 '15 at 16:03
  • @Neha, welcome to SO! To ensure that you get your question answered, please do a search on this site to see if the question has been asked already, then read the [How do I ask a good question](http://stackoverflow.com/help/how-to-ask) page to help make sure your question gets answered (and get those reputation points!) – Ascalonian Jan 29 '15 at 16:09

1 Answers1

1

You have defined that each instance of your custom ArrayList class will itself contain an instance of an ArrayList class. The moment you create one instance, the thread will continue creating infinitely many instances until you get, as you are seeing, a StackOverflowError.

Kon
  • 10,262
  • 6
  • 34
  • 53