Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting element type of generic list

843793May 24 2010 — edited Jun 12 2010
Hello,

I have the following code, which generates a compiler error on the f(x) call:
import java.util.*;

class Test {
  static <T extends Comparable<T>> void f( List<T> l ) {}
  static <E extends List<? extends Comparable<?>>> E g( E x ) {
    // <T>f(java.util.List<T>) in Test cannot be applied to (E)
    f(x);
    return x;
  }
}
my f-method computes the next permutation of the list in-place.
Instead of a method g, I have a class that implements Iterator<E>.
When my iterator is constructed with an ArrayList<Integer>, I want the next()-method to return an ArrayList<Integer>, and the superclass List<Integer>

The Problem here seems to be, that the compiler can't infer that "?" should be "T".

I'm certain my attempt is way too complicated, all I want is to keep f() in the current form, as a generic helper method, and an iterator-class, that computes the permutations using f(), and returns them in the type of the original list.


Thanks for helping!

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 10 2010
Added on May 24 2010
5 comments
196 views