Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:09:47+00:00 2026-06-06T18:09:47+00:00

That is my Java code for a Generic bubbleSorter: public class BubbleSorter<E extends Comparable<E>>

  • 0

That is my Java code for a Generic bubbleSorter:

public class BubbleSorter<E extends Comparable<E>> {
    E[] a;
    void swap(int i, int j) {
        E temp;
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
    void bubbleSort(E[] a) {
        this.a=a;
        for (int i=0 ;i<a.length;i++) {
            for (int j=0;j<a.length;j++) {
                if ( a[i].compareTo(a[j]) > 0) swap(i,j);
            }
        }
    }

}

public interface Comparable<E> {
    public int compareTo(E e);
}

and here is an example of its use:

public class Test { 
    public static void main (String arg[]) {
        Rational[] a = new Rational[3];
        a[0]=Rational.rationalFactory(9,2);
        a[1]=Rational.rationalFactory(1,3);
        a[2]=Rational.rationalFactory(10,11);
        Complex[] b = new Complex[3];
        b[0]=new Complex(7,5);
        b[1]=new Complex(3,4);
        b[2]=new Complex(8,9);
        BubbleSorter<Rational> br=new BubbleSorter<Rational>();
        BubbleSorter<Complex> bi=new BubbleSorter<Complex>();
        br.bubbleSort(a);
        bi.bubbleSort(b);
        for (int i=0 ; i < 3 ; i++ ) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
        for (int i=0 ; i < 3 ; i++ ) {
            System.out.print(b[i] + " ");
        }
    }
}

public class Rational implements Comparable<Rational> {
    int mone,mehane;
    private Rational(int n,int m) {
        mone=n;
        mehane=m;
    }
    static public Rational rationalFactory(int n,int m) {
        if (n==0) return null;
        return new Rational(n,m);
    } 
    public String toString() {
        return mone + "/" + mehane;
    }
    public int compareTo(Rational r) {
        return (r.mehane*mone - r.mone*mehane);
    }
}
public class Complex implements Comparable<Complex> {
        int real,img;
        public Complex(int r,int i) {
            real=r;
            img=i;
        }
        public String toString() {
            return real + "+" + img + "i";
        }
        public int compareTo(Complex r) {
            double x=(getLength() - r.getLength());
            if (x>0) return 1;
            if (x==0) return 0;
            return -1;
        }
        public double getLength() {
            return Math.sqrt(real*real+img*img);
        }
}

When I tried to convert my Java code to C#, I got stuck trying to force the generic type to extend Comparable since < E : Comparable > doesnt work. How can i overcome this?

that is what i tried :

abstract class Comparable<E> {

    static bool operator ==( Comparable<E> e1, Comparable<E> e2 );
    static bool operator !=( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 == e2 );
    }
    static bool operator >( Comparable<E> e1, Comparable<E> e2 );
    static bool operator >=( Comparable<E> e1, Comparable<E> e2 ) {
        if ( e1 > e2 ) return true;
        if ( e1 == e2 ) return true;
        return false;
    }
    static bool operator <=( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 > e2 );
    }
    static bool operator <( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 >= e2 );
    }
}

public class BubbleSorter<E : Comparable<E>> {
        E[] a;
        void swap(int i, int j) {
            E temp;
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        void bubbleSort(E[] a) {
            this.a=a;
            for (int i=0 ;i<a.length;i++) {
                for (int j=0;j<a.length;j++) {
                    if ( a[i]>a[j] ) swap(i,j);
                }
            }
        }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:09:48+00:00Added an answer on June 6, 2026 at 6:09 pm

    You should use the built-in IComparable<T> interface, and then declare your class as

    public class BubbleSorter<T> where T : IComparable<T> { ... }
    

    The where keyword defines a “constraint” on the generic parameter T. The compiler will enforce this constraint by ensuring that for any instantiation of the generic class, the type argument implements the IComparable<T> interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering that simple java code which would not work: public class Bar extends AbstractBar{
Consider this code: public interface Foo extends Comparable<Foo> {} public enum FooImpl implements Foo
So I've got some Java code that uses Jakarta HttpClient like this: URI aURI
Possible Duplicate: Why won't this generic java code compile? Given the following code: import
I'd like to use define a generic class in java, that can only be
I have the following code in an abstract java class: protected abstract <E extends
Why does this java code produce StackOverflowError ? I understand that this somehow connected
I am well aware that generic types are erased from Java code when it
We have method that looks like this : public String getCommandString(Object...args) { return this.code
Newbie Scala Question: Say I want to do this [Java code] in Scala: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.