我的代码有什么问题吗?具体来说,对象项 item2[count]
的数组最初我试图将 item1
插入队列中,但遇到了一个问题,即对象 Item 的先前值存储在队列中被新插入的内容覆盖。我的解决方案是声明一个对象数组 item2[count]
并递增 int count
,现在我收到了 insertFront
Exception 的异常在线程“main”中 java.lang.ArrayIndexOutOfBoundsException: 0
和 insertRear
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0
主要:
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
}
}
最佳答案
你的问题很简单。
当您循环获取用户输入以将另一个 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)
关于java - 创建无限数量的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240520/