通用代码和比较器

时间:2015-10-29 19:40:45

标签: java sorting generics interface comparator

这是通用数组排序代码。 我不知道如何使用setComparator类和BubbleSort中的ArraySort。我一直收到关于“原始类型”的错误......

以下是完整代码:

package sortNumbers;

import java.util.Scanner;
import java.util.Random;

class sortNumbers
{
    public static void main(String[] args)
    {
        int arrayLength = 4;
        String chooseArray = "BubblesSort";
        boolean asORds = true;

        Integer[] arr = new Integer[4];

        Random randomGen = new Random() ;

        for(int i = 0; i<arrayLength; i++)
        {
            arr[i] = randomGen.nextInt(101);
        }

        intComparator ic = new intComparator(asORds);
        BubbleSort bs = new BubbleSort(arr);
        //bs.setComparator(ic);
    }
}

interface Comparator<T>
{
    public int compare(T a, T b);
}

class intComparator<T> implements Comparator<T>
{
    boolean ascend;

    public intComparator(boolean ascend) {
        this.ascend = ascend;
    }

    public int compare(T a, T b)
    {
        int aA = (Integer)a;
        int bB = (Integer)b;
        //int temp;
        int changePosition = 1;

        if(this.ascend == false)
            changePosition = -1;            

        /*if(this.ascend == false)
        {
            temp = aA;
            aA = bB;
            bB = temp;
        }*/

        if(aA == bB)
            return 0*changePosition;
        else if(aA < bB)
            return -1*changePosition;
        else
            return 1*changePosition;
    }
}

class strComparator<T> implements Comparator<T>
{
    protected boolean ascend;

    public strComparator(boolean ascend) {
        this.ascend = ascend;
    }

    public int compare(T a, T b)
    {
        String aA = (String)a;
        String bB = (String)b;
        //int temp;
        int changePosition = 1;

        if(this.ascend == false)
            changePosition = -1;            

        /*if(this.ascend == false)
        {
            temp = aA;
            aA = bB;
            bB = temp;
        }*/

        if(aA.compareTo(bB) == 0)
            return 0*changePosition;
        else if(aA.compareTo(bB) == -1)
            return -1*changePosition;
        else
            return 1*changePosition;
    }
}

class floatComparator<T> implements Comparator<T>
{
    protected boolean ascend;
    public floatComparator(boolean ascend)
    {
        this.ascend = ascend;
    }

    public int compare(T a, T b)
    {    
        float aA = (Float)a;
        float bB = (Float)b;
        //int temp;
        int changePosition = 1;

        if(this.ascend == false)
            changePosition = -1;            

        /*if(this.ascend == false)
        {
            temp = aA;
            aA = bB;
            bB = temp;
        }*/

        if(aA == bB)
            return 0*changePosition;
        else if(aA < bB)
            return -1*changePosition;
        else
            return 1*changePosition;
    }
}
abstract class ArraySort<T>
{
    protected T[] inArray;
    public ArraySort(T[] inArray)
    {
        this.inArray = inArray;
    }

    abstract public void iSort(T[] inArray);
    abstract public T[] oSort(T[] inArray);

    public void setComparator(Comparator<T> comparator)
    {

    }
}

class BubbleSort<T> extends ArraySort<T>
{
    protected Comparator<T> forCompare;

    public BubbleSort(T[] inArray)
    {
        super(inArray);
    }

    public void iSort(T[] inArray)
    {
        T temp;
        for(int i = 0; i < inArray.length-1; i++)
        {
            for(int j=1; j<inArray.length-i; j++)
            {
                //int compared = compare(in)
                if(forCompare.compare(inArray[j-1],inArray[j]) < 0)
                    //    inArray[j-1] > inArray[j])
                {
                    temp=inArray[j-1];
                    inArray[j-1] = inArray[j];
                    inArray[j] = temp;
                }
            }
        }

        if(inArray.length <= 10)
        {
            for(int i = 0; i <inArray.length; i++)
            {
                System.out.print(inArray[i] + " ");
            }
            System.out.println("");
        }
    }

    public T[] oSort(T[] inArray)
    {
        T[] tempArray = (T[]) new Object[inArray.length];
        //is this right way to creat generic type array?
        for(int i = 0; i<tempArray.length; i++)
        {
            tempArray[i] = inArray[i];
        }
        T temp;



        for(int i = 0; i < tempArray.length-1; i++)
        {
            for(int j=1; j<tempArray.length-i; j++)
            {
                if(forCompare.compare(tempArray[j-1],tempArray[j]) < 0)
                {
                    temp=tempArray[j-1];
                    tempArray[j-1] = tempArray[j];
                    tempArray[j] = temp;
                }
            }
        }

        if(tempArray.length <= 10)
        {
            for(int i = 0; i <tempArray.length; i++)
            {
                System.out.print(tempArray[i] + " ");
            }
            System.out.println("");
        }

        return tempArray;
    }

    public void setComparator(Comparator<T> comparator) {
    // How should I use this...please some o
    }
}

BubbleSortoSort方法中,我正在尝试创建一个新的T数组。 这是这样做的吗?

T[] tempArray = (T[]) new Object[inArray.length]; 

1 个答案:

答案 0 :(得分:1)

您正在实施Comparator错误。例如,您的intComparator应声明为:

class intComparator implements Comparator<Integer>
{
    //...
    public int compare(Integer a, Integer b)
    {
        ...
    }
}

在Java中实例化泛型有点棘手。找出原因和选项here

详细了解通用类型here

仅供参考,请记住,班级名称以大写字母开头。