gpt4 book ai didi

Java 代码将新对象的先前条目覆盖到队列数组中

转载 作者:行者123 更新时间:2023-12-01 19:33:50 25 4
gpt4 key购买 nike

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

//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 代码将新对象的先前条目覆盖到队列数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com