2
interface Borrowable {
  Date getCheckoutDate();
  Date getDudeDate();
  void setCheckoutDate(Date d);
  void setDudeDate(Date d);
}

next class has all the functions that an arraylist has. I have tested all of them and they work perfectly.

class LibraryCollection<E> {
  ArrayList<E> items = new ArrayList<>();

  public Iterator<E> iterator() {return items.iterator();}
  boolean add(E o) {return items.add(o);}
  boolean addAll(Collection<? extends E> c) {...}
...
.....
......
}

here is where I cannot figure up how to make " T[] getOverDueBooks(Date date)" to return a valid array. I am trying to do this cause it is a requirement in my java class.

class ChechOutCart<E extends Borrowable> extends LibraryCollection<E> {
  ArrayList<E> dueOnSpecificDate = new ArrayList<>();
  ArrayList<E> dueOn = new ArrayList<>();

  <T> T[] getOverDueBooks(Date date) {
    T[] a = (T[]) java.lang.reflect.Array.newInstance(items.getClass().getComponentType(), items.size());
    Borrowable current;
    DVD d;
    Book b;

    for (int i = 0; i < items.size(); i++) {
      current = this.items.get(i);

      if (current instanceof Book) {
        b = (Book) current;
        if (date.after(b.getDudeDate())) {
          dueOn.add(this.items.get(i));
        }
      } else if (current instanceof DVD) {
        d = (DVD) current;
        if (date.after(d.getDudeDate())) {
          dueOn.add(this.items.get(i));
        }
      }
    } 
    return dueOn.toArray(a);
  }
}

in main I have made a cauple of DVD and Book objects and they are fully initialized I have omitted the initialization cause too long. I have put then in myCar

public static void main(String[] args) {
    ChechOutCart<Borrowable> myCar = new ChechOutCart<>();
    Borrowable[] array;

    DVD dvd1 = new DVD();
    DVD dvd2 = new DVD();
    Book book1 = new Book();
    Book book2 = new Book();

    myCar.add(book2);
    myCar.add(dvd1);
    myCar.add(book1);
    myCar.add(dvd2);

    array = myCar.getOverDueBooks(new Date(115, 03, 23));
    System.out.println(array);
}

and the errors that I got

Exception in thread "main" java.lang.NullPointerException
at java.lang.reflect.Array.newArray(Native Method)
at java.lang.reflect.Array.newInstance(Array.java:75)
at labb_part_b.ChechOutCart.getOverDueBooks(LabB_Part_B.java:40)
at labb_part_b.LabB_Part_B.main(LabB_Part_B.java:147)
Java Result: 1

I am new in java. any idea or examples that I can use to accomplish this task I will appreciate for ever... thanks

stholzm
  • 3,167
  • 17
  • 31
antirrabia
  • 21
  • 2
  • The question title could be improved. Suggestion: 'Why does Class#getComponentType not return the generic type of ArrayList?' Possibly related: [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – stholzm Apr 24 '15 at 16:32

1 Answers1

2

From the Class#getComponentType Javadoc:

Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.

items is an ArrayList, not an array. The generic type of an ArrayList is not accessible at runtime because of Type Erasure.

Not entirely sure why you want to return T[]. You might want to use E instead which already extends Borrowable. And if you really want to return an array, maybe cast after toArray?

meriton
  • 61,876
  • 13
  • 96
  • 163
stholzm
  • 3,167
  • 17
  • 31
  • thanks for your response and your time. I am trying to do this cause it is a requirement in my java class. I know your answer is correct but still I do not understand what is wrong, sorry about that but I'm pretty new at this. thanks again – antirrabia Apr 24 '15 at 19:34
  • I can only guess *what* you don't understand, but perhaps my edit clarified things anyway? – meriton Apr 24 '15 at 20:04
  • 1
    Can you elaborate what exactly you do not understand? Maybe this helps: the component type of an array and the generic type of say an ArrayList are really two different beasts. You can access the component type at runtime with `Class#getComponentType` (only for arrays) but generic types - in general - are not available for introspection. There are some good questions about *Type Erasure* around here you might want to read for a better understanding of Java's type system. Thanks @meriton btw. Oops. – stholzm Apr 24 '15 at 20:13
  • I got my idea how to deal with my problem from a definition that i found on internet that I guess it is the right definition of the function 'public T[] toArray(T[] a) { int size = size(); T[] r = a.length >= size ? a : (T[]) java.lang.reflect.Array.newInstance( etc etc etc ...' so I thought that I can use it in my code to get the array that I want to return in the same way that this function does. but I now know that I am wrong in that line and that helps me a lot because at the beginning I didn't know where I went wrong. thanks both – antirrabia Apr 24 '15 at 23:13