-1

Could it be possible to create a generic and varargs method? I know how to write a varargs method but what if it is generic?I have read a lot of things but I'm confused.

Actually, it is something like this:

    ArrayList <Cell<E>> list=new ArrayList<Cell<E>> ();
    public ArrayList <Cell<E>> sum(ArrayList<Cell<E>> ... elements){
    ....
    }

Is it correct?

jack sparrow
  • 87
  • 1
  • 9
  • I've just posted some code here: http://stackoverflow.com/questions/17378673/how-can-i-open-up-my-equals-statements-to-accept-more-than-one-parameter-in-java/17378881#17378881. Exactly what you want. – tkroman Jun 29 '13 at 19:05
  • 1
    It would be helpful if you included an example of what exactly you're trying to achieve. – NPE Jun 29 '13 at 19:05

4 Answers4

0

You mean, like so (I will use an interface for demonstration)?

public interface Test<T> {

    public void someMethod(T... elements);
}

or

public static <T> someMethod(T... args) {
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

It is possible:

<T> void method(T... args) {
    //logic
}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
0

Of course, the same way you would create a generic method:

public <T> List<T> asList(T... items) {
    return Arrays.asList(items);
}
assylias
  • 321,522
  • 82
  • 660
  • 783
0

Do you mean like this?

  public <T> void varArgsWithGenerics(T... objects){

        }
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56