我的代码出了什么问题?具体来说,对象项item2[count]
的数组原来我试图将item1
插入到队列中,但遇到了一个问题,即存储在que中的对象Item的先前值被新插入的那些值覆盖。我的解决方案是声明一个对象数组item2[count]
并增加int count
,现在我得到了insertFront
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
和insertRear
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
<的例外情况/ p>
主:
public class MyDequeApp {
public static void main(String[] args) {
//variables
String userinNum;
double userinPrice;
int queOp=0;
//???
int count=0;
//creating new Item
Item item1 = new Item();
//array of items!!!????
Item[] item2=new Item[count];
//creating new Scanner
Scanner scan1=new Scanner(System.in);
//user input number of elements in the deque
System.out.println("Enter the number of elements in the que");
int queElm=scan1.nextInt();
MyDeque theQueue=new MyDeque(queElm);
//MyDeque theStack=new MyDeque(queElm);
//do/while so while user selects 1-7 they stay in the switch/case
do {
//switch/case menu
System.out.println("1. Insert to front");
System.out.println("2. Insert to rear");
System.out.println("3. Remove from front");
System.out.println("4. Remove from rear");
System.out.println("5. Peek front");
System.out.println("6. Peek rear");
System.out.println("7. Display deque");
System.out.println("Anything else to Quit");
//user input the case number
queOp=scan1.nextInt();
scan1.nextLine();
//for(int i=0; i<100; i++) { //for start
switch(queOp) {
//insert to front
case 1:
System.out.println("Enter an item number");
userinNum=scan1.nextLine();
item1.setNum(userinNum);
System.out.println("Enter a price");
userinPrice=scan1.nextDouble();
scan1.nextLine();
item1.setPrice(userinPrice);
System.out.println(item1.toString());
item2[count]=item1;
theQueue.insertFront(item2[count]);
count++;
break;
//insert to rear
case 2:
System.out.println("Enter an item numbeR");
userinNum=scan1.nextLine();
item1.setNum(userinNum);
System.out.println("Enter a pricE");
userinPrice=scan1.nextDouble();
scan1.nextLine();
item1.setPrice(userinPrice);
System.out.println(item1.toString());
//item2[count]=item1;
theQueue.insertRear(item2[count]);
count++;
break;
}
//}
}
}
}
班级MyDeque
的方法
public class MyDeque {
private int maxSize;
private Item[] queArray;
private int front;
private int rear;
private int nItems;
//constructor
public MyDeque(int s) {
maxSize = s;
queArray = new Item[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//insertFront()
//For an insertion operation, you have to prompt the user to type in the item#
//and the price. Create an object of the Item and then pass the object as the
//argument to the insertion method
public void insertFront(Item x) {
if(front==maxSize)
front=0;
queArray[front++]=x;
nItems++;
}
//insertRear()
public void insertRear(Item y) {
if(rear==maxSize-1) //wraparound
rear=-1;
queArray[++rear]=y; //CHANGED TO ++rear increment rear and insert
nItems++; //one more item
}
}
答案 0 :(得分:1)
你的问题很简单。
当您循环以从用户获取输入以将另一个Item插入队列或数组时,您需要创建一个新的Item对象:
Item newItem = new Item();
如果您没有创建新项目,那么您只是更改队列中现有项目的值,有效地覆盖它们的值。
您也不需要阵列。
根据我们可能的输入,逻辑应该类似于:
queueOp是一个int,因此您应该调用scan1.nextInt()
Get queueOp from user
switch(queueOp)
case 1:
create new Item and set values (`new Item()`)
call method insertFront(pass new Item)
case 2:
create new Item and set values
call method insertRear(pass new Item)
答案 1 :(得分:0)
您的item2数组初始化为长度为0:
int count=0;
...
Item[] item2=new Item[count];
永远不会分配新的。因此,调用iitem2 [count] = item1将失败,因为您正在访问长度为零的数组的第一个元素(即它没有元素!)。
考虑使用一个List,它会在每次调用时增加(item)。
但是:你为什么要把它存放在那里?你的任务是将它存储在Queue中,不是吗?