JAVA入门——Generic/泛型

  在台科大的第二次JAVA作业,老师课上讲的内容是泛型。

  泛型(generic),泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。通俗点说就是一个盒子本来只能放int,改成泛型之后就可以灵活地根据你的要求放你的类型,如int,double,char,string等。下面看两个例子:

  第一个例子中声明了一个Box类型,并继承了Number,也就是说Box只能用于对数的声明,如int,double,而如果改成3处的代码,则1,2处的代码可以正常运行,因为没有再继承Number,Box泛型也就包括了char,string等。

public class TestDemo2 {

    public static void main(String[] args) {

//1        Box<String> name1 = new Box<String>("corn");
        Box<Integer> name2 = new Box<Integer>(12);
//2        System.out.println("name:" + name1.getData());
        System.out.println("name:" + name2.getData());
    }

}
//3 class Box<T>{
class Box<T extends Number> {

    private T data;

    public Box() {

    }

    public Box(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }

}

运行结果:

name:12

第二个例子声明了一个Point类型,里面包含两个坐标的设置和获取。

public static void main(String[] args){
        // 实例化泛型类
        Point<Integer, Integer> p1 = new Point<Integer, Integer>();
        p1.setX(10);
        p1.setY(20);
        int x = p1.getX();
        int y = p1.getY();
        System.out.println("This point is:" + x + ", " + y);
       
        Point<Double, String> p2 = new Point<Double, String>();
        p2.setX(25.4);
        p2.setY("东京180度");
        double m = p2.getX();
        String n = p2.getY();
        System.out.println("This point is:" + m + ", " + n);
    }
}
// 定义泛型类
class Point<T1, T2>{
    T1 x;
    T2 y;
    public T1 getX() {
        return x;
    }
    public void setX(T1 x) {
        this.x = x;
    }
    public T2 getY() {
        return y;
    }
    public void setY(T2 y) {
        this.y = y;
    }
}

运行结果:

This point is:10, 20
This point is:25.4, 东京180度

  老师布置的是编写一个插入算法的泛型类,对于数据结构非常差的我来说,简直了,不过只有慢慢查资料慢慢改。下面放作业代码。

import java.util.Comparator;





public class LinkedList<T> {
    
    public <T extends Comparable> void sort(T x) {
        Node<T> HEAD=head.next,PRE=head.next;
        Node<T> newNode = new Node(x, null);
        Node<T> transNode= new Node(null,null);

        for(int i=0;i<count;i++){
            
            if(i==0){
                if(x.compareTo(head.data)< 0)//if x<head.data/x字典顺序小于第一个数据
                {
                    newNode = new Node(x, head);
                    transNode.next=newNode;
                    head= transNode.next;
                    count++;
                    break;
                }
                    
                HEAD=head.next;
                continue;
            }
            
            if(i==1){
                if(x.compareTo(HEAD.data)< 0)
                {
                    newNode = new Node(x,HEAD);
                    head.next=newNode;
                    count++;
                    break;
                }
                else
                {
                    HEAD=HEAD.next;
                    PRE=head.next;
                    continue;
                }
            }
            
            if(i<count-1){
                if(x.compareTo(HEAD.data)< 0)
                {
                    newNode = new Node(x,HEAD);
                    PRE.next=newNode;
                    count++;
                    break;
                }
                else{
                    PRE=HEAD;
                    HEAD=HEAD.next;
                }
            }
            
            if(i==count-1){
                if(x.compareTo(HEAD.data)< 0)
                {
                    newNode.next=HEAD;
                    PRE.next=newNode;
                    newNode.data=x;
                    count++;
                    break;
                }
                else
                    HEAD.next=newNode;
                    newNode.data=x;
                    newNode.next=null;
                    count++;
            }

            
        }
      }
    
    
    public static class Node<T> {

        T data;
        Node next;
 
        public Node(T data, Node<T> next) {
            this.data = data;
            this.next = next;
        }

        public String toString() {
            return data.toString();
        }
    }

    
    static int count;
    Node<T> head;

    public LinkedList() {
        count = 0;
        head = null;
    }

    public boolean insert(int index, T entry) {
        if (index < 0 || index > count) {
            return false;
        }
        if (index == 0) {
            Node<T> newNode = new Node(entry, head);
            head = newNode;
            count++;
            return true;
        }
        Node<T> node = head;
        for (int i = 0; i < index - 1; i++, node = node.next);
        //System.out.println("prev "+node);
        Node<T> newNode = new Node(entry, node.next);
        node.next = newNode;
        count++;
        return true;
    }
    
    
    
    
    
    
    
    public int size() {
        return count;
    }
    public boolean isEmpty() {
        return count == 0;
    }
    

    public String toString() {
        StringBuilder s = new StringBuilder();
        for (Node<T> node = head; node != null; node = node.next) {
            if (node != head) {
                s.append(", ");
            }
            s.append(node.toString());
        }
        return s.toString();
    }
    public class Member {
        String firstname;
        String lastname;
        int age;
        public Member(String firstname, String lastname, int age) {
            this.firstname = firstname;
        this.lastname = lastname;
            this.age = age;
        }
        public String toString() {
            return firstname + " " + lastname + " age=" + age;
        }
    }

    public  static void main(String args[]) {
        
        LinkedList<String> fruits = new LinkedList();
        fruits.insert(0, "apple");
        fruits.insert(1, "banana");
        fruits.insert(2, "melon");
        fruits.insert(3, "pear");
        fruits.insert(4, "zzz");
        System.out.println(fruits);
        System.out.println("***********");
        System.out.println("insertion sort:ccc");
        fruits.sort("ccc");
        System.out.println(fruits);
        System.out.print("count:");
        System.out.println(fruits.count);
        System.out.println("------------------");
        
        
        LinkedList<Integer> IntNum = new LinkedList();
        IntNum.insert(0, 1);
        IntNum.insert(1, 3);
        IntNum.insert(2, 5);
        IntNum.insert(3, 7);
        IntNum.insert(4, 9);
        System.out.println(IntNum);
        System.out.println("***********");
        System.out.println("insertion sort:4");
        IntNum.sort(4);
        System.out.println(IntNum);
        System.out.print("count:");
        System.out.println(IntNum.count);
        System.out.println("------------------");

        LinkedList<Double> DouNum = new LinkedList();
        DouNum.insert(0, 1.1);
        DouNum.insert(1, 3.3);
        DouNum.insert(2, 5.1);
        DouNum.insert(3, 7.1);
        DouNum.insert(4, 9.1);
        System.out.println(DouNum);
        System.out.println("***********");
        System.out.println("insertion sort:4.1");
        DouNum.sort(4.1);
        System.out.println(DouNum);
        System.out.print("count:");
        System.out.println(DouNum.count);
        System.out.println("------------------");
         
        
    
    }
}

运行结果:

apple, banana, melon, pear, zzz
***********
insertion sort:ccc
apple, banana, ccc, melon, pear, zzz
count:6
------------------
1, 3, 5, 7, 9
***********
insertion sort:4
1, 3, 4, 5, 7, 9
count:6
------------------
1.1, 3.3, 5.1, 7.1, 9.1
***********
insertion sort:4.1
1.1, 3.3, 4.1, 5.1, 7.1, 9.1
count:6
------------------
原文地址:https://www.cnblogs.com/GoForMyDream/p/5970811.html