“线程中的异常”;“主要”;将对象存储到数组中时发生java.lang.ArrayStoreException
这是我的全部代码,问题需要我使用数组来解决“线程中的异常”;“主要”;将对象存储到数组中时发生java.lang.ArrayStoreException,java,arrays,object,Java,Arrays,Object,这是我的全部代码,问题需要我使用数组来解决 import java.lang.reflect.Array; public class MyStack<T> { public MyStack (Class<T[]> _class,int size){ final T[] values = (T[]) Array.newInstance(_class,size); this.values = values; this.
import java.lang.reflect.Array;
public class MyStack<T> {
public MyStack (Class<T[]> _class,int size){
final T[] values = (T[]) Array.newInstance(_class,size);
this.values = values;
this.size=size;
}
private T[] values;
private int top=0,size;
public void push(T nextElement){
if(isFull()){
System.out.println("full");
}
else {
values[top++] = nextElement;
}
}
public T pop(){
if(isEmpty()) {
System.out.println("empty");
return null;
}
else {
return values[top--];
}
}
public boolean isEmpty(){
if (top==0)return true;
return false;
}
public boolean isFull(){
if(top==size-1)return true;
else return false;
}
public static void main(String args[]){
MyStack<Integer> myStack = new MyStack<Integer>(Integer[].class,9);
for (int i =0;i<10;i++)
{
myStack.push(i);
}
while(!myStack.isEmpty()){
System.out.println(myStack.pop());
}
}
}
import java.lang.reflect.Array;
公共类MyStack{
公共MyStack(类_类,整数大小){
最终的T[]值=(T[])数组;
这个值=值;
这个。大小=大小;
}
私人T[]值;
私有int-top=0,大小;
公共无效推送(T nextElement){
如果(isFull()){
系统输出打印项次(“完整”);
}
否则{
值[top++]=nextElement;
}
}
公共广播电台{
if(isEmpty()){
System.out.println(“空”);
返回null;
}
否则{
返回值[top--];
}
}
公共布尔值为空(){
if(top==0)返回true;
返回false;
}
公共布尔值isFull(){
if(top==size-1)返回true;
否则返回false;
}
公共静态void main(字符串参数[]){
MyStack MyStack=新的MyStack(整数[]。类,9);
对于(int i=0;i你的构造函数需要一个类
,但是应该需要一个类
,而且你不需要在值上有一个变量阴影
public MyStack(Class<T> _class, int size) {
this.values = (T[]) Array.newInstance(_class, size);
this.size = size;
}
或对于isFull
public boolean isFull() {
return top == size - 1;
}
public boolean isFull() {
return top == size - 1;
}