Completely stuck on this error. Here's the class the error is coming from.
/** An array-based Stack. */
public class ArrayStack<E> implements Stack {
/** Array of items in this Stack. */
private E[] data;
/** Number of items currently in this Stack. */
private int size;
/** The Stack is initially empty. */
public ArrayStack() {
data = (E[])(new Object[1]); // This causes a compiler warning
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public Object pop() {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return data[size];
}
public Object peek() {
if (isEmpty()) {
throw new EmptyStructureException();
}
return data[size - 1];
}
/** Return true if data is full. */
protected boolean isFull() {
return size == data.length;
}
public void push(Object target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
/** Double the length of data. */
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
Here's the Stack class just in case it's needed:
/** A last-in, first-out stack. */
public interface Stack<E> {
/** Return true if this Stack is empty. */
public boolean isEmpty();
/**
* Return the top item on this Stack, but do not modify the Stack.
* @throws EmptyStructureException if this Stack is empty.
*/
public E peek();
/**
* Remove and return the top item on this Stack.
* @throws EmptyStructureException if this Stack is empty.
*/
public E pop();
/** Add target to the top of the Stack. */
public void push(E target);
}
The error is in regards to the line data[size] = target;
in the ArrayStack class, in the push(Object target) method.
data[size] = target;
1) Here data
refers to E array
and target
refers to Object
.
Generics brings type safety. So you cannot cannot convert from Object to E.
2) public class ArrayStack<E> implements Stack {
is not correct because you don't provide the parameterized type for the interface you are implementing.
Writing public class ArrayStack<E> implements Stack<E> {
would be safer and will force you to implement the Stack
methods by respecting the parameterized type used in the Stack
methods. For example: public void push(E target);
3) To be conform to declared parameterized type in ArrayStack
you should should use the parameterized type in your method instead of Object
.
so you should replace
public void push(Object target) {
by
public void push(E target) {
And you should do the same thing in all methods you declare that manipulate as declared type Object
instead of E
. For example:
public Object peek()
and
public Object pop() {
Target and data are not of the same type. You can use E
wherhever you need a type. This makes your code more type safe.
Change the push method to:
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
Since your data array is of type E:
private E[] data;
You need to modify:
public void push(Object target) {
to have parameter type E.
E can be String, Integer etc determined at runtime. Object is not equivalent to the runtime type.
Try this:
/**
* An array-based Stack.
*/
public class ArrayStack<E> implements Stack<E> {
/**
* Array of items in this Stack.
*/
private E[] data;
/**
* Number of items currently in this Stack.
*/
private int size;
/**
* The Stack is initially empty.
*/
public ArrayStack() {
data = (E[]) (new Object[1]); // This causes a compiler warning
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public E pop() {
if (isEmpty()) {
throw new RuntimeException();
}
size--;
return data[size];
}
public E peek() {
if (isEmpty()) {
throw new RuntimeException();
}
return data[size - 1];
}
/**
* Return true if data is full.
*/
protected boolean isFull() {
return size == data.length;
}
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
/**
* Double the length of data.
*/
protected void stretch() {
E[] newData = (E[]) (new Object[data.length * 2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.