3
public class Stack {

    public static void main(String[] args) {
        // Strings[]
        // names={"news","ask","man","querty","lang","love","poppye","zebra","hello"};
        int[] names = { 31, 5343, 8776, 90, 123, 33 };// shows me an error
        Selection sec = new Selection();
        sec.sort(names);
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }
    }
}

class Selection {
    int valNow;

    public void sort(Comparable[] names) {
        for (int i = 0; i < names.length; i++) {
            boolean swapOn = false;
            int min = -200;
            int pointer = 0;

            for (int j = i + 1; j < names.length; j++) {

                if (less(names[i], names[j])) {
                    if (valNow > min) {
                        min = valNow;
                        pointer = j;
                        System.out.println("Pointer:" + pointer + " "
                                + "Highest:" + min);
                        swapOn = true;
                    }
                }
            }
            if (swapOn) {
                swap(names, i, pointer);
            }
        }

    }

    public void swap(Comparable[] name, int x, int y) {
        System.out.println("Gonna swap:" + name[x] + " and " + name[y]);
        Comparable inter = name[x];
        name[x] = name[y];
        name[y] = inter;

    }

    public boolean less(Comparable one, Comparable two) {
        boolean send = false;

        valNow = one.compareTo(two);
        System.out.println(valNow);
        if (valNow > 0) {
            send = true;
        } else if (valNow == 0) {
            send = true;
        }

        return send;
    }
}

I am able to pass and sort String array to Comparable[] in selection class sort() method but passing int array to sort() shows me error that Comparable[] is not applicable to int[]!!I need to sort all types of data with same code! Please help me out!

2
  • String implements Comparable interface. int is not an Object, so does not. Commented Dec 5, 2014 at 14:46
  • 1
    Use Integer object and not the primitive int Commented Dec 5, 2014 at 14:49

4 Answers 4

3

Use Integer instead of int. int is not a class and have no compareTo method.

Sign up to request clarification or add additional context in comments.

Comments

2

You should use Integer instead of int.

Comments

1

In Java, arrays are covariant, which means if class C implements interface I then C[] can be treat as I[]. On other hand, String class implements Comparable interface, so an array of Strings can be considered as an array of Comparables, but int[] is an array of primitive int, and int doesn't implement any interface. You should use instead Integer[].

Comments

0

int is a primitive type, while Comparable is a reference type. Use the reference type Integer instead, which is a subtype of Comparable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.