returnItems() is a method that returns an array, not the array itself, so you cannot try to reference the index on it, as you are trying to do here:
System.out.println(animals.returnItems[i]);
What you need to do is reference the index of the array returned from the method as follows:
System.out.println(animals.returnItems()[i]);
Edit
You also had a problem with how you store your data and return it. In your constructor, you create arrayOfItems as an Object array:
arrayOfItems = (T[]) new Object[size];
... but you are trying to return it as an array of T in returnItems() :
public T[] returnItems() { return arrayOfItems; }
When you try to run your code, you will get an exception:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
This is due to what is called type erasure . Basically, the Java compiler will replace all of your generic types with regular classes ( Object in this case) and insert throws to maintain type safety.
So this line from your main() method:
for(int i=0; i < animals.returnItems().length;i++)
will see that animals is a Generic<String> and turns into this:
for(int i=0; i < ((String[])animals.returnItems()).length;i++)
But the array you are returning was created as new Object[] , not a new String[] , and you cannot omit it unless the object actually has a child type, therefore an exception.
To eliminate ClassCastException , you can change returnItems() to declare its actual return type as follows:
public Object[] returnItems() { return arrayOfItems; }
This will not allow the compiler to try to insert an illegal listing of the array, but then you will need to manually translate each element into the corresponding type manually, which will defeat the purpose of using generics in the first place.
As pointed out by JBNizet in the comments above, arrays and generics βdon't mix well,β so you better use ArrayList<T> instead of T[] to store your data.