2016-02-23 4 views
-1

Мне было предложено создать собственный интерфейс deque в java и создать реализацию на основе массива. У меня есть несколько вопросов, почему я могу сделать это определенным образом, или, по крайней мере, почему я не получаю ошибку компилятора в любом случае ...Интерфейсы в Java, Синтаксис, <E>

Меня заинтересовала разница между настройкой класса Pile. Как и у меня есть сделано первоначально ...

public class Pile implements Deque {

, имея мое заявление, как это ...

Pile p = new Pile(6);

против .....

public class Pile<E> implements Deque {

и

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

??? Кроме того, если кто-нибудь увидеть, как я мог бы облажался задание, пожалуйста, не стесняйтесь сообщить ха-ха ...

Вот код для исходного кода для справки ....

Основной метод ==>

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()); 

} 

} 

My Thing класса ==> объекты Im манипулируя ...

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; 
} 
} 

==> Мой Deque интерфейс ...

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(); 
} 

ворса класс ==> Моя реализация массива ...

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; 
} 
} 
+0

Также ... Если я действительно заслуживаю голосования, было бы неплохо узнать, почему ..... –

+2

Для начала вам понадобится 'class Pile implements Deque '. –

ответ

2

Pile.java соединен с Thing.java. В случае использования «< E>» Pile мог содержать объекты, кроме объектов Thing.java, на основе типа во время создания экземпляра Pile.java. Клиентскому коду было бы принудительно добавлять только тип объектов в соответствии с типом, упомянутым во время создания экземпляра объекта Pile.

Здесь В вашем коде, если клиентский код пытается добавить anythig, кроме объектов Thing.java, никто не останавливается на этом. В случае добавления объектов любого другого типа код будет взорваться ClassCastException.

Смежные вопросы