1

Note: This is part of a test I had. The goal is to implement a ListArray on my own, said listarray should be able to store arbitrary types of objects. I am not allowed to use any other library classes such as LinkedList or ListArray that already exists and I am not allowed to ditch the array and implement a Collection. I have to implement a generic array of sorts.

The code below compiles, so I assume that the constructor works. The problem is that when the test asserts whether listArray[0] equals list.append(1) I get a nullPointerException. I think that the problem is that I need to create a new Object and insert that into the array, the problem is that I don't know what Object T will be, and therefore don't understand how I can create a new T with argument "element".

public class List<T> implements Seq<T> {

public T[] listArray;
public int size;
public int freeIndex;

public List(){
    this.listArray = (T[])new Object[10];
    this.size = 10;
    this.freeIndex = 0;
}

//have to create a new object
public void append(T element){
    this.listArray[0] = element; //need some sort of new T() method.
}
}

UPDATE I realize that append lacks functionality, that is the intention, I am solely focued on inserting an arbitrary Object T into the array.

UPDATE1 I was overly focused with generics and missed that my constructor was wrongly written. I apologize if it seemed lazy, I have been trying to solve this for several hours but alas, I'm quite inexperienced in Java. I nonetheless got an answer to the problem of insterting an element to the generic Array. Thank you.

Ricardo DM
  • 33
  • 5
  • What is the problem exactly? Which method calls the constructor? I can't find it. Can you please post a reproducible example of code? – Yassin Hajaj Apr 03 '18 at 18:30
  • 1
    List() is not a constructor. It's a method. Becouse you added `void` as the return type. Voting to close as typo. Next time, do a tiny bit of debugging. Add Sysout statements, or just execute your code with your debugger. Such mistakes are really easy to find. – JB Nizet Apr 03 '18 at 18:31
  • You are not incrementing your array index when you insert – bcr666 Apr 03 '18 at 18:32
  • `public void List()` is not a constructor, try `public List()` – A. Markóczy Apr 03 '18 at 18:32
  • I'm sorry I did the constructor wrong, I was too focused on understanding generics that I didn't see that. – Ricardo DM Apr 03 '18 at 19:06

2 Answers2

1

If you try debugging you would see that this.listArray is null. Probably your intention for public void List() was to be constructor but it is not constructor. It is ordinary method. be public List() (no return type).

You definitely do not need new T() method to solve this problem (it is possible, see Create instance of generic type in Java?).

Finally you will soon find that your append method works as nothing else 'put in first position'. In order to have complete append implementation you need

  • update freeIndex
  • resize array if it is too small to fit new element

You do not really need size as member variable, you could rely of listArray.length.

Bartosz Bilicki
  • 10,502
  • 8
  • 63
  • 98
-1

You need to increment your array index when you insert

public class List<T> implements Seq<T> {

    private int incrementSize = 10;
    private T[] listArray = new T[incrementSize];
    private int size = 10;
    private int freeIndex = 0;

    public List(){
    }

    //have to create a new object
    public void append(T element){
        this.listArray[freeIndex] = element; //need some sort of new T() method.
        freeIndex++;
        if (freeIndex >= size) {
            increaseArraySize();
        }
    }

    private void increaseArraySize() {
        // create a new array with incrementSize extra buckets
        // copy old array into new array
        // point old array at new array
        // increment size
    }
}
Ricardo DM
  • 33
  • 5
bcr666
  • 1,839
  • 10
  • 20
  • Your member variables should be made protected or private, not public. If they are made public, someone can change them from outside your object and cause problems. – bcr666 Apr 03 '18 at 18:44