创建无限量的对象以放入双端队列

时间:2016-03-26 00:57:19

标签: java queue deque

我正在尝试创建一个用户定义的deque,用于存储用户定义的数量对象。我创建一个item对象并给它赋值或item#和price,而不是将对象存储在双端队列中。到目前为止,我的主要由一个单一项目组成。每次我设置项目时,它都会使用最新的项目值写入先前存储的项目值。我怎样才能创建一个无限量的项目来存储它们?

主要

import java.*;
import java.util.*;

public class MyDequeApp {

public static void main(String[] args)
{
    //variables
    String userinNum;
    double userinPrice;
    int queOp;

    //creating new Item
    Item item1= new Item();

    //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);

    //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();

        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());
                theQueue.insertFront(item1);
                    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());
                theQueue.insertFront(item1);
                theQueue.insertRear(item1);
                    break;

            //remove from front
            case 3:
                theQueue.removeFront();
                    break;

            //remove from rear
            case 4:
                theQueue.removeRear();
                    break;

            //peek front
            case 5:
                System.out.println(theQueue.peekFront());
                    break;

            //peek rear
            case 6:
                System.out.println(theQueue.peekRear());
                    break;

            //display the deque
            case 7: 
                System.out.println(theQueue.toString());
                    break;

            //quit
            default:
                    break;


        }

    }while(queOp>0 && queOp<8); 

}

}

1 个答案:

答案 0 :(得分:0)

您需要添加

item1= new Item(); 

在完成一个并将其添加到您的队列之后,或者在您输入新数据之前的某个位置,否则程序认为item1始终是相同的项目。