日期:2014-05-20  浏览次数:20671 次

高手求教!!Java写的泛型冒泡法
Java code

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

class ArrayBubble<E>
{
    private E[] a;
    private int nElements;

    public ArrayBubble(int max)
    {
        a = (E[])new Object[max];
        nElements = 0;
    }

    public void insert(E value)
    {
        a[nElements++] = value;
    }

    public void display()
    {
        for (int j = 0;j < nElements; j++)
        {
            System.out.print(a[j] + " ");
        }
        System.out.println();
    }

    public E[] getArray()
    {
        return a;
    }

}

class BubbleSort<E extends Comparable<E>>
{
    public void sort(E[] a, int nElements)
    {
        for (int out = 0; out < nElements - 1; out++)
        {
            for (int in = 0; in < nElements - out - 1; in++)
            {
                if (a[in].compareTo(a[in + 1]) > 0)
                {
                    E temp = a[in];
                    a[in] = a[in + 1];
                    a[in + 1] = temp;
                }
            }
        }
    }
}

public class BubbleSortApp
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数组大小: ");
        int size = scanner.nextInt();
        Random rnd = new Random();

        ArrayBubble<Integer> myArray = new ArrayBubble<Integer>(size + 10);
        for (int i = 0; i < size ;i++)
        {
            myArray.insert((Integer)rnd.nextInt(100));
        }
        
        System.out.println("原数组如下:");
        myArray.display();
        new BubbleSort<Integer>().sort(myArray.getArray(), size);
        System.out.println("排序后的数组如下");
        myArray.display();
    }
}



------解决方案--------------------
Java code
public ArrayBubble(int max, Class<E> clazz) //加一个类型参数
    {
        //a = (E[])new Object[max];
        a = (E[])Array.newInstance(clazz, max); //用反射的方式生成实例
        nElements = 0;
    }

//调用
ArrayBubble<Integer> myArray = new ArrayBubble<Integer>(size + 10, Integer.class);

------解决方案--------------------
Java code

import java.util.*;
class ArrayBubble<E>
{
    private E[] a;
    private int nElements;
    
    //构造器改成这样
    //构造器改成这样
    //构造器改成这样
    public ArrayBubble(E[] a)
    {
        this.a = a;
        nElements = 0;
    }
    public void insert(E value)
    {
        a[nElements++] = value;
    }

    public void display()
    {
        for (int j = 0;j < nElements; j++)
        {
            System.out.print(a[j] + " ");
        }
        System.out.println();
    }

    public E[] getArray()
    {
        return a;
    }
}

class BubbleSort<E extends Comparable<E>>
{
    public void sort(E[] a, int nElements)
    {
        for (int out = 0; out < nElements - 1; out++)
        {
            for (int in = 0; in < nElements - out - 1; in++)
            {
                if (a[in].compareTo(a[in + 1]) > 0)
                {
                    E temp = a[in];
                    a[in] = a[in + 1];
                    a[in + 1] = temp;
                }
            }
        }
    }
}

public class BubbleSortApp
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数组大小: ");
        int size = scanner.nextInt();
        Random rnd = new Random();
        //下面一行改成这样
        //下面一行改成这样
        //下面一行改成这样
        ArrayBubble<Integer> myArray = new ArrayBubble<>(new Integer[size+10]);
        for (int i = 0; i < size ;i++)
        {
            myArray.insert((Integer)rnd.nextInt(100));
        }
        
        System.out.println("原数组如下:");
        myArray.display();
        new BubbleSort<Integer>().sort(myArray.getArray(), size);
        System.out.println("排序后的数组如下");
        myArray.display();
    }
}