0

I get following compilation errors:

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (T[], Comparator<capture#1-of ? extends T>) Main.java   /Sorting/src/com/kash/test  line 11
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (T[], Comparator<capture#2-of ? super T>)   Main.java   /Sorting/src/com/kash/test  line 15
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (T[], Comparator<T>)    Main.java   /Sorting/src/com/kash/test  line 19

when I compile (with JDK 1.7.0 in Eclipse Juno) this code:

package com.kash.test;

import java.util.Collections;
import java.util.Comparator;

// 3 attempts to write a wrapper over: Collections' method
//      public static <T> void sort(List<T> list, Comparator<? super T> c)
public class Main {

    public static <T> void sort1(T[] array, Comparator<? extends T> c) {
        Collections.sort(array, c); // line 11
    }

    public static <T> void sort2(T[] array, Comparator<? super T> c) {
        Collections.sort(array, c); // line 15
    }

    public static <T> void sort3(T[] array, Comparator<T> c) {
        Collections.sort(array, c); // line 19
    }

    public static void main(String[] args) {

    }
}

I have read following but did not really understand much. Any help?

-- edit --

For the picky guys who would want to know why am I doing such stupid things. If you don't care why am I doing this, don't bother the rest.

Why?

I'm writing several implementations (e.g. Quick, Insertion, Heap, Mixed, Intro, Bubble, ...) of following interface:

package com.kash.src;

import java.util.Comparator;
import java.util.List;

/**
 * Interface that provides all sorting functions for all possible representations of a list of Objects (non-primitive
 * types). <br>
 * - {@link List}<{@link Comparable}><br>
 * - Array of < ? extends {@link Comparable}><br>
 * - {@link List}< ? > with external {@link Comparator}<br>
 * - Array of < ? > with external {@link Comparator}<br>
 * 
 * @author Kashyap Bhatt
 * 
 */

public interface Sorter {
    <T extends Comparable<T>> void sort(List<T> list);

    <T extends Comparable<T>> void sort(T[] array);

    <T extends Object> void sort(List<T> list, Comparator<? super T> c);

    <T extends Object> void sort(T[] array, Comparator<? super T> c);
}

So that I can test all my sort implementations and test them. I wanna compare the results with Java's sort implementations, so I'm also writing an implementation of this interface which internally just calls Java's sort methods. That's where I faced the problem.

Community
  • 1
  • 1
Kashyap
  • 15,354
  • 13
  • 64
  • 103

3 Answers3

3

Collections#sort expects a List while you are passing an array.
Do:

public static <T> void sort1(T[] array, Comparator<? extends T> c) {
        Collections.sort(Arrays.asList(array), c); // line 11
}
Cratylus
  • 52,998
  • 69
  • 209
  • 339
3

Collections.sort() does not sort arrays, but Lists.

Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
1

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (T[], Comparator<capture#1-of ? extends T>) Main.java /Sorting/src/com/kash/test line 11

You are passing T[] where List<T> is expected and Comparator<? extends T> where Comparator<? super T> is expected.

Why do you think it should not give error?

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133