Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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通配符捕获错误,包含泛型堆栈数组_Java_Arrays_Generics_Stack_Bounded Wildcard - Fatal编程技术网

JAVA通配符捕获错误,包含泛型堆栈数组

JAVA通配符捕获错误,包含泛型堆栈数组,java,arrays,generics,stack,bounded-wildcard,Java,Arrays,Generics,Stack,Bounded Wildcard,这段代码从文本文件中获取信息,并将对象推送到堆栈中 我得到一个错误,说: 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].equa

这段代码从文本文件中获取信息,并将对象推送到堆栈中

我得到一个错误,说:

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];
    }
}
堆栈中的推送(捕获#627 of?)无法应用于(床) 我创建的所有类都会重复此错误

注释掉的部分是以前我为每个对象创建单个堆栈时使用的代码


在将所有内容都推入堆栈后,我无法将所有内容都放入数组,因为不必要的中间变量将被取点。

正确执行此操作并安全键入的唯一方法是

a) 将所有这些组合成一个
堆栈
(假设所有类都扩展
家具


b) 为每个堆栈保留单独的变量,并完全删除数组。

正确执行此操作并安全键入的唯一方法是

a) 将所有这些组合成一个
堆栈
(假设所有类都扩展
家具

b) 为每个堆栈保留单独的变量,并完全删除数组。

试试这个

push(capture#627 of ?) in Stack<capture#627 of ?> cannot be applied to (Bed)
((堆栈)堆栈[0])。推送(新床(临时[1],临时[2]);
试试这个

push(capture#627 of ?) in Stack<capture#627 of ?> cannot be applied to (Bed)
((堆栈)堆栈[0])。推送(新床(临时[1],临时[2]);

尝试过,但没有成功。此外,作业中不允许使用“b”选项。我说这是正确或安全打字的唯一方法时,我是认真的。另一种方法是丢失类型安全性并开始执行不安全的原始强制转换:
(堆栈)堆栈[0]。推送
将接受任何
对象
。嗯,是的,我不知道为什么任务需要这个。看起来这是在教糟糕的设计,真的。如果我被要求像这样检查代码,我会拒绝继续检查,直到这被修复。尝试了,没有效果。此外,作业中不允许使用“b”选项。我说这是正确或安全打字的唯一方法时,我是认真的。另一种方法是丢失类型安全性并开始执行不安全的原始强制转换:
(堆栈)堆栈[0]。推送
将接受任何
对象
。嗯,是的,我不知道为什么任务需要这个。看起来这是在教糟糕的设计,真的。如果我被要求像这样检查代码,我会拒绝继续检查,直到修复为止。
((Stack<Bed>)stacks[0]).push(new Bed(temp[1], temp[2]));