Я написал класс для реализации двусторонней очереди с использованием выровненного по левому краю массива.Я также хочу написать класс для реализации двусторонней очереди, используя "циклический" метод.Я сказал, что так как он поделится некоторыми методами, я сделаю его подклассом.Когда я пытаюсь переопределить метод вставки первым, я получаю ошибку времени компиляции.
CircularArrayBasedDeque.java:6: reference to insertFirst is ambiguous, both method insertFirst(EltType) in ArrayBasedDeque and method insertFirst(EltType) in CircularArrayBasedDeque match
dq.insertFirst(i);
^
CircularArrayBasedDeque.java:21: method does not override or implement a method from a supertype
@Override
^
ArrayBasedDeque.java
public class ArrayBasedDeque <EltType>
implements Deque <EltType>
{
public static void main( String[] args )
{
ArrayBasedDeque dq = new ArrayBasedDeque();
for( int element = 0; element < 64; element ++)
{
dq.insertFirst(element);
System.out.println(dq);
}
/**for( int element = 0; element < 25; element ++)
{
dq.insertLast(element);
System.out.println(dq);
}
*/
for( int element = dq.size(); element > 0; element --)
{
dq.removeFirst();$
System.out.println(dq);$
}
}
private final int INITIAL_CAPACITY = 2;
private int capacity;
private EltType elements[];
private int first;
private int last;
public ArrayBasedDeque()
{
capacity = INITIAL_CAPACITY;
elements = ( EltType[] ) ( new Object[INITIAL_CAPACITY] );
first = -1;
last = 0;
}
/**
* Returns the size of the Deque by
* returning the index of the first element + 1.
* @return the deque size.
*/
public int size()
{
return first + 1;
}
/**
* Returns true if and only if the deque is empty.
* @return true/false indication of emptiness.
*/
public boolean isEmpty()
{
return ( first + 1 == last );
}
/**
* Return the first element of the deque;
* illegal if the deque is empty.
* @return the front element.
*/
public EltType first()
{
if(isEmpty())
{
System.err.println("You cannot remove an element from an empty double sided queue");
}
return elements[first];
}
/**
* Return the last element of the deque;
* illegal if the deque is empty.
* @return the last element.
*/
public EltType last()
{
if(isEmpty())
{
System.err.println("You cannot remove an element from an empty double sided queue");
}
return elements[last];
}
/**
* Insert item at the front of the deque.
* @param element: the item to be added.
*/
public void insertFirst( EltType element )
{
if(isFull())
{
expand();
}
elements[first + 1] = element;
first++;
}
/**
* Insert item at the rear the deque.
* @param element: the item to be added.
*/
public void insertLast( EltType element )
{
if(isFull())
{
expand();
}
for( int index = first + 1; index > 0; index-- )
{
elements[index] = elements[ index - 1 ];
}
elements[0] = element;
first++;
}
/**
* Return and remove the front element of the deque;
* Illegal if deque is empty.
*/
public EltType removeFirst()
{
if(isEmpty())
{
System.err.println("You cannot remove an element from an empty double sided queue");
}
if(isQuarterFull())
{
contract();
}
return elements[first--];
}
/**
* Return and remove the last element of the deque;
* illegal if the deque is empty.
*/
public EltType removeLast()
{
if(isEmpty())
{
System.err.println("You cannot remove an element from an empty double sided queue");
}
if(isQuarterFull())
{
contract();
}
EltType last = elements[0];
for (int index = 0; index < first; index ++)
{
elements[index] = elements[ index + 1 ];
}
return last;
}
/**
* Return true if and only if the queue is full.
* @return boolean representsion of weather the queue is full.
*/
public boolean isFull()
{
return ( size() == capacity );
}
/**
* Return true if and only if the queue is quarter full
* @return boolean representation of weather the queue is quarter full.
*/
public boolean isQuarterFull()
{
return ( size() == capacity / 4 );
}
/**
* Doubles the capacity of the array representing the queue
*/
public void expand()
{
EltType[] tmp;
tmp = ( EltType[] ) ( new Object[this.size() * 2] );
for( int element = 0; element < capacity ; element++ )
{
tmp[element] = elements[element];
}
capacity *= 2;
elements = tmp;
}
/**
* Halves the capacity of the array representing the queue
*/
public void contract()
{
EltType[] tmp;
tmp = ( EltType[] ) ( new Object[ capacity / 2 ] );
for ( int element = 0; element < size(); element++ )
{
tmp[element] = elements[element];
}
capacity /= 4;
elements = tmp;
}
/**
* Returns a string representation of the deque.
* @return a string representation of the deque.
*/
@Override
public String toString()
{
String string = "{ ";
for (int index = 0; index <= first; index ++)
{
string = string + elements[index] + " ";
}
string = string + "}";
string = string + "\n";
string = string + "Size " + size();
string = string + "\n";
string = string + "Capacity " + capacity;
string = string + "\n";
return string;
}
}
CircularArrayBasedDeque.java
public class CircularArrayBasedDeque <EltType>
extends ArrayBasedDeque
{
public static void main( String[] args )
{
CircularArrayBasedDeque dq = new CircularArrayBasedDeque();
for ( int i = 0; i < 20; i++ )
{
dq.insertFirst(i);
System.out.println(dq);
}
}
private final int INITIAL_CAPACITY = 20;
private int capacity;
private int first;
private int last;
private EltType[] elements;
public CircularArrayBasedDeque()
{
capacity = INITIAL_CAPACITY;
elements = ( EltType[] ) ( new Object[INITIAL_CAPACITY] );
first = 0;
last = 0;
}
@Override
public void insertFirst( EltType element )
{
}
@Override
public boolean isEmpty()
{
return ( first == last );
}
@Override
public int size()
{
return ( capacity - first + last );
}
}
СпасибоЗа вашу помощь заранее.Я новичок в ООП и считаю дизайн классов и наследование непонятными.