私のコードで何が問題になっていますか?具体的には、オブジェクト項目item2[count]
元の配列item1
をキューに挿入しようとしましたが、キューに格納されているオブジェクトItemの以前の値が新しく挿入されたオブジェクトによって上書きされていた問題が発生しました。私のソリューションは、オブジェクトitem2[count]
の配列を宣言し、int count
をインクリメントすることだったと今私はinsertFront
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
とinsertRear
Exception in thread "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;
}
//}
}
}
}
方法
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
}
}
item1をキューに挿入し続けると、以前に挿入されたアイテムが上書きされます。私は配列からそれらを保存すれば、私は前のものを書くことよりも新しい価値の形を保つことができると思った – Dan
はい、pczeusの答えを参照してください! – Heri