2

In my most recent class assignment we've been working with generics and I have been receiving this warning:

Note: Selector.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details. 

From what I understand this results from not specifying the type of objects in my collection. I'm currently working with int's for testing purposes, but the program is supposed to allow for sorting of any type object. I don't think I want to specify my collections as int's, and my collection is currently of type T for this reason. But my instructions explicitly state that my program should not generate any warnings. Here is one of my methods:

What I have now:

     else {
        T[] a = new T[c.size()];
        c.toArray(T[] a);
        T min = a[0];
        for (int i = 0; i < a.size(); i++) {
           if (comp.compare(min, a[i]) > 0) {
              min = a[i];
           }
        }
        return min; 
     }

What I'm getting:

    Testing.java:48: error: generic array creation
            T[] a = c.toArray(new T[c.size()]);
Evan F
  • 61
  • 1
  • 6
  • 10
  • 3
    Well did you try taking the compiler's advice, to see the details? – Jon Skeet Feb 01 '13 at 23:39
  • 1
    And you *really* don't want to be calling `toArray` inside a loop like that, by the way. (You might also want to consider that `Collection` extends `Iterable`...) – Jon Skeet Feb 01 '13 at 23:40
  • A `Notice` is not a `Warning`: http://docs.oracle.com/cd/E12483_01/wlevs20/config_server/logging.html#wp1013043 – Nir Alfasi Feb 01 '13 at 23:44

1 Answers1

2

To get rid of the notice you should use

Collection.toArray(T[] a)

instead of

Collection.toArray() 

Collection.toArray() returns an array of Objects, so you will always need to cast.

Also, calling toArray is expensive, because new array must be created, therefore you should consider storing it into variable and reusing.

Unfortunately it is not possible to create an array of generic type, therefore I think that you should remove the toArray and use iterator over the collection, for example:

Iterator<T> iterator = c.iterator();
T min = iterator.next(); //you already checked that there has to be first element
while (iterator.hasNext()) {
    T element = iterator.next();
    if (comp.compare(min, element) > 0) {
        min = element;
    }
}
return min;
zibi
  • 3,183
  • 3
  • 27
  • 47
  • What exactly is (T[] a) supposed mean to me? I read through the API and tried making some adjustments, but I guess I'm entirely clear on the difference in the outcome of calling toArray() vs. toArray(T[] a). – Evan F Feb 02 '13 at 00:22
  • T[] a, means an array of type T, method toArray returns Object, because it has to be backwards compatible with previous version, therefore second method toArray(T[] a) was created if someone wants to retrieve array of type T. As described in the javadoc: T[] a is the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose – zibi Feb 02 '13 at 00:28
  • Unfortunately it is not possible to create an array of generic type... therefore I would suggest using iterator over the collection – zibi Feb 02 '13 at 01:06
  • Please check updated anwser – zibi Feb 02 '13 at 01:15
  • Great! You can consider accepting my answer:) – zibi Feb 02 '13 at 02:06