Java 无法对类型T[]调用迭代器
我试图在数据结构的实例方法中迭代数据结构的元素。以下是我的数据结构及其方法的代码:Java 无法对类型T[]调用迭代器,java,Java,我试图在数据结构的实例方法中迭代数据结构的元素。以下是我的数据结构及其方法的代码: import java.util.Iterator; public class DropOutStackArray<T> implements DropOutStack<T>{ private static final int CAP = 10; private int bottom, top, size; private T[] cstack; p
import java.util.Iterator;
public class DropOutStackArray<T> implements DropOutStack<T>{
private static final int CAP = 10;
private int bottom, top, size;
private T[] cstack;
public static void main(String[] args){
//System.out.println(2%10);
//int[] a = new int[10];
//System.out.println(a.length);
}
private class MyIterator implements Iterator<T>{
private int curr = 0;
@Override
public boolean hasNext() {
return this.curr != DropOutStackArray.this.size;
}
@Override
public T next() {
if(hasNext()){
return cstack[curr++];
}
return null;
}
public void remove(){
if(curr == 0)
return;
cstack[--curr] = cstack[--size];
cstack[curr] = null;
}
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return new MyIterator();
}
public DropOutStackArray(){
this.cstack = (T[]) new Object[CAP];
this.bottom = 0; this.top = 0;
this.size = 0;
}
public DropOutStackArray(final int INCAP){
this.cstack = (T[]) new Object[INCAP];
this.bottom = 0; this.top = 0;
this.size = 0;
}
@Override
public void push(T data) {
// TODO Auto-generated method stub
if(this.size == this.cstack.length){
this.cstack[bottom] = data;
this.bottom = (this.bottom + 1) % this.cstack.length;
this.top = (this.top + 1) % this.cstack.length;
}
this.cstack[this.top] = data;
this.top = (this.top + 1) % this.cstack.length;
this.size++;
}
@Override
public T pop(){
T popped;
if(!isEmpty()){
int length = this.cstack.length;
this.top = (this.top + length - 1) % length;
popped = this.cstack[this.top];
this.cstack[this.top] = null;
this.size--;
}else{
throw new StackEmptyException();
}
return popped;
}
@Override
public T peek() {
// TODO Auto-generated method stub
if(isEmpty()){
throw new StackEmptyException();
}
T peeked = this.cstack[this.top-1];
return peeked;
}
@Override
public int size() {
return this.size;
}
@Override
public boolean isEmpty() {
if(this.size == 0){
return true;
}
return false;
}
public String toString(){
Iterator<T> itr = this.cstack.iterator();
}
}
import java.util.Iterator;
公共类DropOutStack数组实现DropOutStack{
专用静态最终积分上限=10;
私人int底部、顶部、尺寸;
私人T[]cstack;
公共静态void main(字符串[]args){
//系统输出打印项次(2%10);
//int[]a=新的int[10];
//系统输出打印长度(a.长度);
}
私有类MyIterator实现了迭代器{
私有int curr=0;
@凌驾
公共布尔hasNext(){
返回this.curr!=DropOutStackArray.this.size;
}
@凌驾
公共交通工具{
if(hasNext()){
返回cstack[curr++];
}
返回null;
}
公共空间删除(){
如果(当前==0)
返回;
cstack[--curr]=cstack[--size];
cstack[curr]=null;
}
}
@凌驾
公共迭代器迭代器(){
//TODO自动生成的方法存根
返回新的MyIterator();
}
公共DropOutStackArray(){
this.cstack=(T[])新对象[CAP];
this.bottom=0;this.top=0;
此值为0.size=0;
}
公共DropOutStackArray(最终整数增量){
this.cstack=(T[])新对象[INCAP];
this.bottom=0;this.top=0;
此值为0.size=0;
}
@凌驾
公共无效推送(T数据){
//TODO自动生成的方法存根
if(this.size==this.cstack.length){
this.cstack[bottom]=数据;
this.bottom=(this.bottom+1)%this.cstack.length;
this.top=(this.top+1)%this.cstack.length;
}
this.cstack[this.top]=数据;
this.top=(this.top+1)%this.cstack.length;
这个.size++;
}
@凌驾
公共广播电台{
弹跳;
如果(!isEmpty()){
int length=this.cstack.length;
this.top=(this.top+长度-1)%length;
popped=this.cstack[this.top];
this.cstack[this.top]=null;
这个尺寸--;
}否则{
抛出新的StackEmptyException();
}
返回弹出;
}
@凌驾
公共T peek(){
//TODO自动生成的方法存根
if(isEmpty()){
抛出新的StackEmptyException();
}
T peek=this.cstack[this.top-1];
回眸;
}
@凌驾
公共整数大小(){
返回此.size;
}
@凌驾
公共布尔值为空(){
如果(this.size==0){
返回true;
}
返回false;
}
公共字符串toString(){
迭代器itr=this.cstack.Iterator();
}
}
我的问题在于最后一个方法-toString()。当我尝试创建itr时,我会在标题中显示错误。为什么我不能在cstack上调用迭代器?您的两个方法
toString()
和iterator()
都是DropOutStackArray
类中的实例方法
因此,您应该更改以下内容-
Iterator<T> itr = this.cstack.iterator();
Iterator itr=this.cstack.Iterator();
到
Iterator itr=this.Iterator();
我假设数组不提供迭代器。这里有一个类似的问题:不幸的是,我不能使用任何集合接口。我已经编写了自己的迭代器类,我不得不使用它。
Iterator<T> itr = this.iterator();