1

EDIT: For anyone stumbling upon this in the future, OHGODSPIDERS phrased the question better in the comments, saying, "His intention is to change the method toArray to public T[] toArray() which will throw an error because the method toArray() of List returns an Object[]". the issue is that java does not let you (without workarounds) create Arrays of generic types (e.g. T).

So I am trying to implement a method in a class that returns an Array representation of the data contained (it is for an assignment to practice binary search trees). This is the relevant code (I think):

public ArrayList<T> toArrayList() {
    // TODO Auto-generated method stub.
    return this.root.toArrayList();
}

public Object[] toArray() {
    // TODO Auto-generated method stub.
    return this.toArrayList().toArray();
}

Note specifically the return type of the toArray() method. I am trying to figure out why when I set the return type to T[] instead Eclipse gets mad and throws an error. I think that it makes more sense though for it to be T. Both the toArrayList() and toArray() methods pass the provided unit tests as written.

Thank you in advance for your help! Please let me know if there is anything I can clarify. I know this is a basic question but this is the first time in my coding career Google hasn't had a good answer for me!

I posted the following as a comment but I figured I would put it here as well so I can copy my new code too: (If this is discouraged please let me know and I will make whatever changes suggested):

@OHGODSPIDERS exactly. Sorry I was not clearer. I read through the links and I think I understand now that I should use the toArray(T[] a) version instead. However, when I try to do that Eclipse says I can't create a generic array. However, isn't that exactly what the method says to do?

    public T[] toArray(){
    return this.toArrayList().toArray(new T[0]);
}
  • What error? It is unclear what you are asking, what are you trying, what are you experiencing, is the show code "fine" or is this the code with "problems"? What is the alternative that you want / don't want but works / doesn't work? – luk2302 Dec 29 '17 at 14:12
  • I can't understand your intention - why should you write the method ArrayList to ArrayList? – zlakad Dec 29 '17 at 14:14
  • 1
    His intention is to change the method `toArray` to `public T[] toArray()` which will throw an error because the method `toArray()` of List returns an `Object[]` – OH GOD SPIDERS Dec 29 '17 at 14:15
  • see this question: https://stackoverflow.com/questions/36598928/java-generics-in-arraylist-toarray – TmTron Dec 29 '17 at 14:17
  • To return an array of type `T[]`, you need to supply an instance of `T[]` to your `toArray` method, and pass that to the `toArray` method of `this.toArrayList()`. – Andy Turner Dec 29 '17 at 14:18
  • @OHGODSPIDERS exactly. Sorry I was not clearer. I read through the links and I think I understand now that I should use the toArray(T[] a) version instead. However, when I try to do that Eclipse says I can't create a generic array. However, isn't that exactly what the method says to do? I will put my new code in the problem description. – Justin Calareso Dec 29 '17 at 14:38
  • @JustinCalareso Unfortunatly java doesn't allow for the creation of a generic array which makes this whole thing more complicated. There are some ways to work around it but none are really great: [How to create a generic array in Java?](https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – OH GOD SPIDERS Dec 29 '17 at 14:41
  • @OHGODSPIDERS That makes sense. Why would the javadocs say T[] if it can't exist? Are they just saying that you can put whatever object you want but it has to be specified? – Justin Calareso Dec 29 '17 at 14:45
  • The Javadocs say `T[]` because it is a method parameter that takes an array of type T (aka the type which the list uses). You can use generic arrays like that and for example recieve them as a parameter of a method, you just can not create them. So for a `List` the parameter would have to be a `String[]`: `new ArrayList().toArray(new String[0]);`. Problem is just when the Type T is yet unspecified and still generic as in your case. – OH GOD SPIDERS Dec 29 '17 at 15:01

0 Answers0