0

This is my first class MyArrayList with the method addToEnd that I am trying to run.

public class MyArrayList <E extends Comparable<E>>implements List<E> {
    private E[] data;
    private int size = 0;


    private void reallocate(){
        size = 2 * size;
        data = Arrays.copyOf(data, size);

    }


    public boolean addToEnd(E e) {

    if (size == data.length){
        reallocate();
    }
    data[size++] = e;
    System.out.println();
    return true;


}

My second class, ArraySorterTester, is where I am trying to call my addToEnd method, but for some reason it won't work and I keep getting an error.

public class ArraySorterTester {
    public static void main(String [] args){
        Integer[] test = new Integer[] {1, 2, 4, 6, 8 };
        MyArrayList<Integer> inserter = new MyArrayList<Integer>();
        boolean sorted = true;


        inserter.addToEnd(10);
        for (int k = 0; k < test.length; k++) {  
               System.out.print(test[k] + " "); 
        }
}   
  • 1
    What error do you get? – Lesleh Feb 07 '15 at 05:54
  • at MyArrayList.addToEnd(MyArrayList.java:42) ArraySorterTester.main(ArraySorterTester.java:25) it tells me what lines are wrong, but I have no clue how to fix it... I think it might have something to do with how I'm calling it. Or perhaps I wrote this line wrong MyArrayList inserter = new MyArrayList(); But I really don't know what I'm doing wrong – Hiroki Drake Feb 07 '15 at 05:56

1 Answers1

0

You haven't initialized private E[] data, so accessing data.length in addToEnd will result in an error.

Try creating a constructor for MyArrayList and initializing private E[] data there with new. Remember to make size the same as data.length initially.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I don't understand how I would go about doing this, but I do see your point since I haven't initialized private E[] data. – Hiroki Drake Feb 07 '15 at 06:39
  • @HirokiDrake Read up on constructors and how to initialize an array in Java. – Emil Laine Feb 07 '15 at 06:43
  • Ok so I would do something like "public MyArrayList(Comparable data){ data = new E(new int[]{10,20,30,40,50}); }" I'm still confused though, is there a way I can make put data into a constructor but declare the array inside my ArraySorterTester class? – Hiroki Drake Feb 07 '15 at 07:09
  • @HirokiDrake Yes, something like that: `data = new E[size];` – Emil Laine Feb 07 '15 at 07:18
  • If you want to do something like `MyArrayList ints = new MyArrayList(1, 2, 3, 4, 5);` to initialize `data` to contain `{ 1, 2, 3, 4, 5 }` read up on *varargs*. – Emil Laine Feb 07 '15 at 07:21
  • I've never learned about varargs yet but from what I've looked up, I'm unsure of how I could use this since many of the examples given don't cover generic type arrays and data is a generic type. Any hints you could give me? And thanks a bunch for you help. – Hiroki Drake Feb 07 '15 at 07:41
  • @HirokiDrake Check [this](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) question for information about generic arrays in Java. – Emil Laine Feb 07 '15 at 07:46
  • ok, but how would I initialize data to contain what MyArrayList inserter holds? – Hiroki Drake Feb 07 '15 at 08:18
  • @HirokiDrake Here's a quick demonstration I made for using varargs: http://ideone.com/uKDSMl – Emil Laine Feb 07 '15 at 08:49