我正在尝试编写一种算法,该算法允许程序的用户将订单输入到作为数组的队列中。我可以插入订单并插入另一个订单,但是,我下的每个后续订单都会覆盖以前的订单。阵列中仍然有订单的位置,但是它们都是最新订单的副本。这是我的代码:

//Class to define individual orders for queueOrders
class Order
{

    //Global Variables
    public static String name;
    public static String order;
    public static int orderNum;

    //Constructor
    public Order()
    {

    }

    public Order(int orderNum)
    {
        this.orderNum = orderNum;
    }

    public Order(String name, String order)
    {
        this.name = name;
        this.order = order;
    }

    public Order(String name, String order, int orderNum)
    {
        this.name = name;
        this.order = order;
        this.orderNum = orderNum;
    }

    //Getters and Setters
    public static String getName()
    {
        return name;
    }

    public static void setName(String name)
    {
        Order.name = name;
    }

    public static String getOrder()
    {
        return order;
    }

    public static void setOrder(String order)
    {
        Order.order = order;
    }

    public static int getOrderNum()
    {
        return orderNum;
    }

    public static void setOrderNum(int orderNum)
    {
        Order.orderNum = orderNum;
    }

}


//Class to define queue
class QueueOrders
{

    //Global Variables
//    private int nItems = 0;

    private static int maxSize;
    private static int numOfOrders = 0;

    //Array of Orders objects
    private static Order orders[];

    private static int front;
    private static int back;

    //Constructor
    public QueueOrders(int size)
    {
        maxSize = size;
        orders = new Order[maxSize];
        front = 0;
        back = 0;

    }

 //Insert new order
    public static void insertOrder(String name, String order)
    {
        //Variables
//        int cntr = 1;


        if(isFull())
        {
            System.out.println("There are too many orders."
                    + " Remove some first. ");
        }
        else
        {
//            if(back == maxSize - 1)
//            {
//                back = 0;
//            }


            Order.setName(name);
            Order.setOrder(order);
 ////        Order.name = name;
////        Order.order = order;
//        Order.setOrderNum(cntr);



        Order newOrder = new Order(name, order, (numOfOrders + 1));

        //Add order to orders array
        orders[back] = newOrder;
        //front = numOfOrders - 1;

//        cntr++;
        back++;
        numOfOrders++;

        }
    }

//These functions are in the main class of the program

 public static void main(String[] args)
    {
        QueueOrders queue = new QueueOrders(100);
        menu();
    }

  //Function to add an order to the queue
    public static void addOrder()
    {
        //Variables
        String xName;
        String xOrders;
        Scanner myScan = new Scanner(System.in);

        try
        {
            //Message user
            System.out.println("What is the three letter name for your order? ");

            xName = myScanner.nextLine();

            //Message user
            System.out.println("What is your order? ");

            xOrders = myScan.nextLine();

            QueueOrders.insertOrder(xName, xOrders);

            QueueOrders.displaySingleOrder();

            System.out.println("Your order has been placed. ");

        } catch (Exception e)
        {
            System.out.println("Was unable to insert your order");
        }
    }

最佳答案

问题是您将静态变量用于订单数据:

public static String name;
public static String order;
public static int orderNum;


它们特定于类Order,而不特定于要在列表中添加的对象Order order。从这些变量中删除关键字static

这个实现

public class Sample {
    public String a;
    public static String b;

    public static void main(String[] args) {
        Sample s1 = new Sample();
        s1.a = "1a";
        s1.b = "1b";
        Sample s2 = new Sample();
        s2.a = "2a";
        s2.b = "2b"; // This one set Sample.b to "2b" because b is
                     // static and shared among all Sample objects (s1 and s2).
        System.out.println(s1.a + " " + s1.b + " " + s2.a + " " + s2.b);
    }
}


将打印1a 2b 2a 2b(不是1a 1b 2a 2b),因为b是静态的,并且"1b"的值被s2.b = "2b"覆盖。

有关更多信息,请参见this article

关于java - Java代码将新对象的先前条目覆盖到队列数组中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59239929/

10-10 12:30