2016-03-26 13 views
0

私のコードで何が問題になっていますか?具体的には、オブジェクト項目item2[count]元の配列item1をキューに挿入しようとしましたが、キューに格納されているオブジェクトItemの以前の値が新しく挿入されたオブジェクトによって上書きされていた問題が発生しました。私のソリューションは、オブジェクトitem2[count]の配列を宣言し、int countをインクリメントすることだったと今私はinsertFrontException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0insertRearException 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 
    } 
} 

答えて

1

あなたを問題は簡単です。

あなたがキューまたは配列に別の項目を挿入するために、ユーザからの入力を取得するためにループしているとき、あなたは新しいItemオブジェクトを作成する必要があります:あなたは新しいアイテムを作成しない場合

Item newItem = new Item(); 

を、キュー内の既存の項目の値を変更するだけで、効果的に値を上書きすることになります。

この配列も必要ありません。私たちの可能な入力に基づいて

、のようなものになるはずロジック: queueOpはint型であるので、あなたはあなたのITEM2アレイが長さ0で初期化され 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) 
0

を呼び出すする必要があります。

int count=0; 
... 
Item[] item2=new Item[count]; 

新しいものは割り当てられません。したがって、長さがゼロの配列の最初の要素にアクセスしているので(つまり、要素がありません!)、iitem2 [count] = item1の呼び出しは失敗します。

add(item)の呼び出しごとに増加するリストを使用することを検討してください。

:なぜそこに保存しますか?あなたの仕事はそれを貯蓄することです。

+0

item1をキューに挿入し続けると、以前に挿入されたアイテムが上書きされます。私は配列からそれらを保存すれば、私は前のものを書くことよりも新しい価値の形を保つことができると思った – Dan

+0

はい、pczeusの答えを参照してください! – Heri

関連する問題