Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将ArrayList添加到嵌套在映射中的队列_Java_Collections - Fatal编程技术网

Java 将ArrayList添加到嵌套在映射中的队列

Java 将ArrayList添加到嵌套在映射中的队列,java,collections,Java,Collections,对作业的解释: 仅使用Collections框架,编写一个允许用户买卖股票的程序。我不允许使用用户定义的类 股票需要有一个3个字符的字符串来表示股票名称、所述股票的整数金额以及所述股票的售价 如果用户从同一家公司购买了两个股票实例,则首先购买的股票将首先出售。先进先出 我尝试创建一个带有字符串键的映射和一个带有嵌套ArrayList作为值的队列。我想将带有股票价格和股票金额的ArrayList推送到队列中。我认为FIFO队列非常适合跟踪和处理同一股票名称下的大量购买。字符串键将是股票名称,它的所

对作业的解释: 仅使用Collections框架,编写一个允许用户买卖股票的程序。我不允许使用用户定义的类

股票需要有一个3个字符的字符串来表示股票名称、所述股票的整数金额以及所述股票的售价

如果用户从同一家公司购买了两个股票实例,则首先购买的股票将首先出售。先进先出

我尝试创建一个带有字符串键的映射和一个带有嵌套ArrayList作为值的队列。我想将带有股票价格和股票金额的ArrayList推送到队列中。我认为FIFO队列非常适合跟踪和处理同一股票名称下的大量购买。字符串键将是股票名称,它的所有购买都将按顺序记录在它下面

不幸的是,我似乎无法找到一种方法来实际访问队列,而不a)覆盖队列中已经存在的内容,或者b)将队列限制为一个无法绑定到股票名称的实例

似乎我需要一个队列的多个独立实例,每个实例位于不同的字符串键下,但我不知道如果没有用户定义的类,如何正确实现

您可以在提供的代码中看到嵌套集合的其他尝试。所有这些都受到以下事实的限制:我缺少一些关于收藏导航的信息,或者我对收藏的理解总体上有缺陷

public static void buy( Map<String, Queue< ArrayList<Integer> >> stockInfo, Queue< ArrayList<Integer> > stockQueue ) {
    System.out.print("Please enter a set of three characters: ");
    Scanner scanner = new Scanner(System.in);

    var stockName = scanner.nextLine();

    while (stockName.length() != STOCK_NAME_COUNT) {
        System.out.printf("Invalid number of characters: %d \n", stockName.length());
        System.out.print("Please enter a set of three characters: ");
        stockName = scanner.nextLine();
    }

    System.out.print("Please enter number of stocks: ");
    var stockNumber = scanner.nextInt();
    while (stockNumber <= 0) {
        System.out.printf("Invalid stock amount: %d \n", stockNumber);
        System.out.print("Please enter number of stocks: ");
        stockNumber = scanner.nextInt();
    }

    System.out.print("Please enter stock price: ");
    var stockPrice = scanner.nextInt();
    while (stockPrice <= 0) {
        System.out.printf("Invalid price: %d \n", stockPrice);
        System.out.print("Please enter stock price: ");
        stockNumber = scanner.nextInt();
    }

    ArrayList<Integer>shares=new ArrayList<Integer>();
    shares.add(stockNumber);
    shares.add(stockPrice);
    stockQueue.add(shares);
    stockInfo.put(stockName, stockQueue);
}

...

public static void main(String[] args) {

    //Set<String> stockNames = new HashSet<String>();
    //Set<Queue< ArrayList<Integer> >> StockInfo = new HashSet<Queue< ArrayList<Integer> >>();

    Map<String, Queue< ArrayList<Integer> >> stockInfo = new LinkedHashMap<String,Queue< ArrayList<Integer> >>();
    Queue< ArrayList<Integer> > stockQueue = new LinkedList< ArrayList<Integer> >();

    //Set<Queue< ArrayList<Integer> >> stockInfo = new HashSet<Queue< ArrayList<Integer> >> ();

    //Queue< ArrayList<Integer> > stockInfo = new LinkedList< ArrayList<Integer> >();

    var selection = menu("Choose option: ");

    while (selection != 'E'){
        if (selection == 'B'){
            buy(stockInfo, stockQueue);
            System.out.println(stockInfo);
        } else {
            //sale(stockInfo);
        }
        selection = menu("Choose option: ");
    }

    System.out.println("Goodbye!");
}
publicstaticvoidbuy(Map>stockInfo,QueuestockQueue){
System.out.print(“请输入一组三个字符:”);
扫描仪=新的扫描仪(System.in);
var stockName=scanner.nextLine();
while(stockName.length()!=STOCK\u NAME\u COUNT){
System.out.printf(“无效字符数:%d\n”,stockName.length());
System.out.print(“请输入一组三个字符:”);
stockName=scanner.nextLine();
}
系统输出打印(“请输入股票数量:”);
var stockNumber=scanner.nextInt();
while(stockNumber();
Map>stockInfo=newlinkedhashmap>();
QueuestockQueue=newlinkedlist();
//Set>stockInfo=newhashset>();
//QueuestockInfo=newlinkedlist();
变量选择=菜单(“选择选项:”);
while(选择!=“E”){
如果(选择=='B'){
购买(股票信息、股票队列);
System.out.println(stockInfo);
}否则{
//销售(股票信息);
}
选择=菜单(“选择选项:”);
}
System.out.println(“再见!”);
}
a、 购买(B或B)

b、 出售

c、 出口(E或E)

选择选项:b

请输入一组三个字符:asd

请输入股票数量:120

请输入股票价格:10

{asd=[[120,10]]}

a、 购买(B或B)

b、 出售

c、 出口(E或E)

选择选项:b

请输入一组三个字符:asd

请输入股票数量:100

请输入股票价格:14

{asd=[[120,10],[100,14]]

a、 购买(B或B)

b、 出售

c、 出口(E或E)

选择选项:b

请输入一组三个字符:asd

请输入股票数量:120

请输入股票价格:10

{asd=[[120,10],[100,14],[120,10]]

a、 购买(B或B)

b、 出售

c、 出口(E或E)

选择选项:b

请输入一组三个字符:qwe

请输入股票数量:50

请输入股票价格:13

{asd=[[120,10],[100,14],[120,10],[50,13]],qwe=[[120,10], [100,14],[120,10],[50,13]]

a、 购买(B或B)

b、 出售

c、 出口(E或E)

选择选项:e

再见

我没有遇到语法错误,我不确定我心目中的逻辑是否适用于我当前的工具集

如果您查看我的输出,您可以看到我的ArrayList队列在每个字符串键上都发生了更改。我知道这是为什么,但如果我在函数中声明一个队列的新实例,它将覆盖该键下已有的内容。

在您的
buy()中
method,您总是覆盖映射中的队列。您需要首先尝试检索现有队列(否则创建一个新队列),然后在必要时对其进行修改:

ArrayList<Integer>shares=new ArrayList<Integer>();
shares.add(stockNumber);
shares.add(stockPrice);

// this retrieves the existing queue, or creates and stores a new one if there wasn't one already
Queue<ArrayList<Integer>> stockQueue = stockInfo.getOrDefault(stockName, new LinkedList<ArrayList<Integer>>());

stockQueue.add(shares);
ArrayListshares=new ArrayList();
股份。添加(股票编号);
股票。加上(股票价格);
//这将检索现有队列,如果还没有队列,则创建并存储新队列
Queue stockQueue=stockInfo.getOrDefault(stockName,newlinkedlist());
stockQueue.add(股票);
然后您可以删除这一行:
stockInfo.put(stockName,stockQueue);
因为
getOrDefault()
方法将新创建的队列放入映射中

您需要从
buy()
方法签名中删除stockQueue参数