0

My list impl:

public class TestMyList implements List{
     int index=0;
     int size;
     Object [] element;
     public TestMyList(){
     element=new Object[10];
  }

  @Override
  public boolean add(Object ele) {
    element[index++]=ele;
    size=index;
    return true;
  }

}

class which implements my custom list:

public class TestClass   {

  public static void main(String[] args) {

    TestMyList list=new TestMyList();
    list.add("hello");
    list.add("i am here");
    System.out.println(list.size());


    System.out.println(list);

  }

}

when i run the above program, it displays mypack.TestMyList@2c84d9, instead of [hello, i am here]

please help me in resolving this

pradeep
  • 295
  • 4
  • 17

1 Answers1

1
 System.out.println(list);

When you call System.out.println, That apparently calls the toString() method of your list Object. Since you didn't ovveride toString(), it calls the default implementation. Ovveride toString() method to print as you wish.

To implement just look at the source code of ArrayList and see how toString() ovverriden.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307