简体   繁体   English

带有通用堆栈数组的JAVA通配符捕获错误

[英]JAVA Wildcard Capture Error with an array of generic stacks

Stack<?>[] stacks = {
    new Stack<Bed>(),
    new Stack<Bookshelves>(),
    new Stack<Chair>(),
    new Stack<Desk>(),
    new Stack<Table>()
};

That's the code making the array of stacks. 这就是构成堆栈数组的代码。 The reason I'm putting them in an array is because it's one of the requirements for the Assignment. 我将它们放在数组中的原因是,这是分配的要求之一。 The program works without the array. 该程序无需数组即可工作。

Also, the Stack takes in generics because I had to create my own Stack (also a requirement of the Assignment). 另外,堆栈采用了泛型,因为我必须创建自己的堆栈(也是分配的要求)。

while(sc.hasNext()){
    temp = sc.nextLine().split(" ");
    if(temp[0].equals("Bed")){
        //beds.push(new Bed(temp[1], temp[2]));
        stacks[0].push(new Bed(temp[1], temp[2]));
    }else if(temp[0].equals("Table")){
        //tables.push(new Table(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), temp[4]));
        stacks[4].push(new Table(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), temp[4]));
    }else if(temp[0].equals("Desk")){
        //desks.push(new Desk(temp[1],temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4])));
        stacks[3].push(new Desk(temp[1],temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4])));
    }else if(temp[0].equals("Chair")){
        //chairs.push(new Chair(temp[1], temp[2]));
        stacks[2].push(new Chair(temp[1], temp[2]));
    }else if(temp[0].equals("Bookshelves")){
        //bookshelves.push(new Bookshelves(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), temp[5]));
        stacks[1].push(new Bookshelves(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), temp[5]));
    }else{
        color = temp[0];
    }
}

This code gets the information from the text file and pushes the Objects into the stack. 此代码从文本文件获取信息,并将对象压入堆栈。

I get an Error saying: 我收到一条错误消息:

push(capture#627 of ?) in Stack<capture#627 of ?> cannot be applied to (Bed)

This Error repeats for all the Classes that I have created. 对于我创建的所有类,都会重复出现此错误。

The parts commented out is the code that worked before when I made a single stack for each Object. 注释掉的部分是我为每个对象制作单个堆栈之前起作用的代码。

I can't put everything into the array after I have pushed everything into the Stack because points will be taken out for unnecessary intermediate variables. 在将所有内容都压入堆栈后,我无法将所有内容都放入数组,因为不必要的中间变量将被删除。

The only way to do this properly and type-safely is either 正确且安全地执行此操作的唯一方法是

a) combine this all into one Stack<Furniture> (assuming the classes all extend Furniture ) a)将所有这些组合到一个Stack<Furniture> (假设所有类都扩展了Furniture

b) keep separate variables for each stack, and get rid of the array entirely. b)为每个堆栈保留单独的变量,并完全摆脱数组。

尝试这个?

((Stack<Bed>)stacks[0]).push(new Bed(temp[1], temp[2]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM