首页 > 解决方案 > 表达式的类型必须是数组类型,但它解析为对象 java

问题描述

我想从每个bills[][]数组中打印出索引 1 处的数字。例如,7 英寸bills[0][1] = new Bill("Carrot", 7, 2000)。它说“表达式的类型必须是数组类型,但它解析为比尔。” 我应该怎么办?

package Module6;

    public class Bill {
        
        String name;
        int numberCheckedOutPerItem;
        int pricePerItem;
        
        
        Bill (String name, int ...v){
            this.name = name;
            this.numberCheckedOutPerItem =numberCheckedOutPerItem;
            this.pricePerItem = pricePerItem;
        }
    
    }

package Module6;

    public class CashRegister {
        
        private Bill[][] bills; 
        CashRegister(){
            bills = new Bill[4][3];
            
            bills[0][0] = new Bill("Cucumber", 4, 5000);
            bills[0][1] = new Bill("Carrot", 7, 2000);
            bills[0][2] = new Bill("Onion", 11, 1500);
            
            bills[1][0] = new Bill("Cucumber", 8, 5000);
            bills[1][1] = new Bill("Carrot", 18, 2000);
            bills[1][2] = new Bill("Onion", 3, 1500);
            
            bills[2][0] = new Bill("Cucumber", 2, 5000);
            bills[2][1] = new Bill("Carrot", 7, 2000);
            bills[2][2] = new Bill("Onion", 6, 1500);
            
            bills[3][0] = new Bill("Cucumber", 9, 5000);
            bills[3][1] = new Bill("Carrot", 13, 2000);
            bills[3][2] = new Bill("Onion", 20, 1500);
            
        }
    
    **void getItems() {
        int totalNum=0;
        for (int i=0; i<bills.length; i++) {
            for(int j=0; j<bills[i].length; j++) {
                for(int k=0; k<bills[i][j].length; k++) {
                totalNum = totalNum + (int) bills[i][j][2];
                }**
            }
        }   
    }

标签: javaarraystypecasting-operator

解决方案


推荐阅读