Java中的接口,语法,<e>

时间:2016-02-23 19:25:47

标签: java interface deque

我被要求在java中创建自己的deque接口,并创建一个基于数组的实现。我有几个问题,为什么我可以采取某些方式,或者至少为什么我不会因为这两种方式而出现编译错误......

我对设置我的桩类之间的区别感兴趣,就像我最初做的那样...

public class Pile implements Deque {

虽然我的声明是这样的......

Pile p = new Pile(6);

VS .....

public class Pile<E> implements Deque {

Pile<Thing> p = new Pile<>(6);

???如果有人看到我如何搞砸了作业,请随时告知哈哈......

以下是我原始代码的代码供参考....

主要方法==&gt;

    public class Tester {

/**
* @param args the command line arguments
 */
public static void main(String[] args) {
    Thing a = new Thing();
    Thing b = new Thing();
    Thing c = new Thing();
    Thing d = new Thing();
    Thing e = new Thing();
    Thing f = new Thing();
    Thing g = new Thing();
    Thing h = new Thing();
    Thing i = new Thing();
    Thing j = new Thing();
    Thing k = new Thing();
    Thing l = new Thing();
    Thing m = new Thing();
    Thing n = new Thing();
    Thing o = new Thing();

    System.out.println("Populating Pile.....");
    Pile p = new Pile(6);
    p.addLast(a);
    p.addLast(b);
    p.addLast(c);

    System.out.println("Current status...");
    System.out.println(p.toString());
    System.out.println("Tests....");

    //--- Test... peak*()
    System.out.println();
    System.out.println("peakFront() = 1 : " + p.peakFirst().toString());
    System.out.println("peakBack() = 3 : " + p.peakLast().toString());
    System.out.println("\nCurrent status...");
    System.out.println(p.toString());

    //--- Test... addLast()
    System.out.println();
    System.out.println("Adding Element 4 to back....");
    p.addLast(d);
    System.out.println("peakBack() = 4 : " + p.peakLast().toString());
    System.out.println("\nCurrent status...");
    System.out.println(p.toString());

    //--- Test... removeLast()
    System.out.println();
    System.out.println("Removing Element 4 from back....");
    System.out.println("removeBack() = 4 : " + p.removeLast().toString());
    System.out.println("peakBack() = 3 : " + p.peakLast().toString());
    System.out.println("\nCurrent status...");
    System.out.println(p.toString());

    //--- Test... addFirst();
    System.out.println();
    System.out.println("Adding Element 4 to the front...");
    p.addFirst(d);
    System.out.println("peakFront() = 4 : " + p.peakFirst().toString());
    System.out.println("\nCurrent status...");
    System.out.println(p.toString());

    //--- Test... removeFirst()
    System.out.println();
    System.out.println("Removing Element 4 from the front");
    System.out.println("removeFront() = 4 : " + p.removeFirst().toString());
    System.out.println("peakFront() = 1 : " + p.peakFirst().toString());
    System.out.println("\nCurrent status...");
    System.out.println(p.toString());

    //--- Test... Tests if Size will grow dynamically at runtime...
    System.out.println();
    System.out.println("Current initial pile size set to " + p.getSpaceA());
    System.out.println("Overloading Pile...");
    p.addLast(d);
    p.addLast(e);
    p.addLast(f);
    p.addLast(g);
    p.addLast(h);
    p.addLast(i);
    p.addLast(j);
    p.addLast(m);
    p.addLast(n);
    p.addLast(o);
    System.out.println("\nCurrent status...");
    System.out.println(p.toStringWithNulls());
    System.out.println("expect 24 | recieve: " + p.getSpaceA());

    //--- Test... Tests if Size will shrink dynamically at runtime...
    System.out.println();
    System.out.println("Removing elements from pile....");
    for (int x = 0; x < 5; x++) {
        p.removeLast();
    }
    System.out.println("expect 12 | recieve: " + p.getSpaceA());
    System.out.println("\nCurrent status...");
    System.out.println(p.toStringWithNulls());

}

}

我的事物类==&gt;对象我操纵......

    public class Thing {
private static int total;
private final int ID;

public Thing(){
    total++;
    ID = total;
}

public int getID() {
    return ID;
}

@Override
public String toString() {
    return "Thing Number: " + ID;
}   
}

==&GT;我的双端接口...

    public interface Deque<E> {

// As far as choosing the names of these operations I just googles, 
// and found what somebody on the internet claimed as common names
// I tried to put the actual java term in the comments beseide each
// Inserts element at the end
public void addLast(E target);

// Removes Element ar the end
public E removeLast();

// Returns back element with out removing it
public E peakLast();


// Insert Element at the front
public void addFirst(E target);

// Remove first element
public E removeFirst();

// Returns front element with out removing it
public E peakFirst();

// Returns the sixe of the Deque
public int size();

// Return true if empty. false if contains any elements
public boolean isEmpty();
}

桩类==&gt;我的阵列实现......

    public class Pile implements Deque {

private int size;
Thing pile[];

public Pile(int _intitalSize) {
    size = -1;
    pile = new Thing[_intitalSize];

}

//adds a element to the last slot of the deque
@Override
public void addLast(Object target) {

    size++;
    if (checkIfTooSmall()) {
        pile = increaseSpace();
    }
    pile[size] = (Thing) target;

}

//removes from deque and reurns the last item in the deque
@Override
public Object removeLast() {
    Thing t = (Thing) pile[size];
    size--;
    if (checkIfTooBig()) {
        pile = decreaseSpace();
    }
    return t;
}

//Returns last object without removing the object from the deque
@Override
public Object peakLast() {
    return pile[size];
}

//adds a new object to the array in the first slot, while shifting data down
@Override
public void addFirst(Object target) {
    size++;
    if (checkIfTooSmall()) {
        pile = increaseSpace();
    }

    Thing[] tempPile = new Thing[pile.length + 1];
    tempPile[0] = (Thing) target;

    for (int i = 1; i < tempPile.length; i++) {
        tempPile[i] = pile[i - 1];
    }
    pile = tempPile;

}

//removes and returns first object
@Override
public Object removeFirst() {
    Thing t = (Thing) pile[0];
    Thing[] tempPile = new Thing[pile.length - 1];

    for (int i = 0; i < tempPile.length; i++) {
        tempPile[i] = pile[i + 1];
    }
    pile = tempPile;
    size--;
    if (checkIfTooBig()) {
        pile = decreaseSpace();
    }
    return t;
}

//returns first object without removing it from array
@Override
public Object peakFirst() {
    return pile[0];
}

//returns size index of array
@Override
public int size() {
    return size;
}

//checks if array is empty
@Override
public boolean isEmpty() {
    return size == -1;
}

//Check to determine if array is full
public boolean checkIfTooSmall() {
    if (size == pile.length) {
        return true;
    } else {
        return false;
    }
}

//doubles array size by copying data into a new larger array
public Thing[] increaseSpace() {

    Thing[] tempPile = new Thing[pile.length * 2];

    for (int i = 0; i < pile.length; i++) {
        tempPile[i] = pile[i];
    }

    return tempPile;
}

//check if size index is small enough to half array size
public boolean checkIfTooBig() {
    if ((pile.length / 2) > size) {
        return true;
    } else {
        return false;
    }
}

//halves array size by copying data into a new smaller array
public Thing[] decreaseSpace() {

    Thing[] tempPile = new Thing[pile.length / 2];

    for (int i = 0; i < tempPile.length; i++) {
        tempPile[i] = pile[i];
    }

    return tempPile;
}

//returns size of actual array
public int getSpaceA() {
    return pile.length;
}

//Shows space available in null form
public String toStringWithNulls(){
    String r = "";
    for(int i = 0; i < pile.length; i++){
        r = r + String.format(pile[i] + " |\n");
    }
    return r;
}

//shows only space populated with objects
@Override
public String toString(){
    String r = "";
    for(int i = 0; i <=  size(); i++){
        r = r + String.format(pile[i] + " |\n");
    }
    return r;
}
}

1 个答案:

答案 0 :(得分:2)

Pile.java与Thing.java耦合。如果&#39;&lt; E&gt;&#39;如果使用了Pile,那么在实例化Pile.java期间,Pile可以根据类型包含除Thing.java对象之外的实体。客户端代码将被强制仅根据&#39;类型&#39;添加对象类型。在实例化Pile对象时提到。

这里在您的代码中,如果客户端代码尝试添加除Thing.java对象之外的任何方法,则没有人停止这样做。如果添加了任何其他类型的对象,则代码会因ClassCastException而爆炸。