数组常量只能用于初始化错误 [英] Arrays constants can only be used in initializers error

查看:32
本文介绍了数组常量只能用于初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

public proj 3 {

    static string [][]Item;

    public static void main(String [] args){

        Item[][] = {
             {"BH," , "Backhoe,"            , "200.00"},
             {"ER," , "Electric Rake,"      ,  "10.00"},
             {"EL," , "Electric Lawnmower," ,  "15.00"},
             {"TR," , "Trencher"            ,  "35.00"},
             {"MU," , "Mulcher,"            ,  "20.00"},
             {"TS," , "Tree Sprayer,"       ,  "22.00"},
             {"CP," , "Cider Press,"        ,  "30.00"},
             {"PR," , "Pruner,"             ,  "12.00"},
             {"GE," , "Gas Edger,"          ,  "20.00"},
             {"RO," , "Roller,"             ,   "8.00"},

如何才能使用不同的方法调用数组?

How can I make it so I can call the array from a different method?

推荐答案

这意味着你必须像这样初始化它:

That means you have to initialize it like this:

public class Proj3{

    public static String [][] Item = {
              {"BH," , "Backhoe," , "200.00"},
              {"ER," , "Electric Rake," , "10.00"},
              {"EL," , "Electric Lawnmower," , "15.00"},
              {"TR," , "Trencher" , "35.00"},
              {"MU," , "Mulcher," , "20.00"},
              {"TS," , "Tree Sprayer," , "22.00"},
              {"CP," , "Cider Press," , "30.00"},
              {"PR," , "Pruner," , "12.00"},
              {"GE," , "Gas Edger," , "20.00"},
              {"RO," , "Roller," , "8.00"}
         };

public static void main(String [] args){
     ...
}

如果要使用数组初始化器,则不能拆分声明和赋值.

If you want to use the array initializer, you cannot split the declaration and assignment.

这篇关于数组常量只能用于初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆