0

是否可以通过使用数组和 for 循环来实例化下面显示的数据库变量,因此不必输入每一行?

 // example of one line only

 public static final String A0 = "a0";

如果有数百行有没有办法更好地做到这一点?

 public class DatabasePrototype {

    public static final String DATABASE_NAME = "database";
    public static final String SMALL_TANK_CHECK_RESULT = "small_tank_check_result";
    public static final String LARGE_TANK_CHECK_RESULT = "large_tank_check_result";

    public static final String CLEAN_TANK_RESULT = "clean_tank_result";
    public static final int DATABASE_VERSION = 1;
    public static final String _ID = "_id";

    // variables for LARGE_TANK_CHECK_RESULT
    public static final String A0 = "a0";
    public static final String A1 = "a1";
    public static final String A2 = "a2";
    public static final String A3 = "a3";
    public static final String A4 = "a4";
    public static final String A5 = "a5";
    public static final String A6 = "a6";
    public static final String A7 = "a7";
    public static final String A8 = "a8";
    public static final String A9 = "a9";
    public static final String A10 = "a10";
    public static final String A11 = "a11";
    public static final String A12 = "a12";
    public static final String A13 = "a13";
    public static final String A14 = "a14";
    public static final String A15 = "a15";
    public static final String A16 = "a16";
    public static final String A17 = "a17";
    public static final String A18 = "a18";
    public static final String A19 = "a19";
    public static final String A20 = "a20";
    public static final String A21 = "a21";
    public static final String A22 = "a22";
    public static final String A23 = "a23";
    public static final String A24 = "a24";
    public static final String A25 = "a25";
    public static final String A26 = "a26";
    public static final String A27 = "a27";
    public static final String A28 = "a28";
    public static final String A29 = "a29";
    public static final String A30 = "a30";
    public static final String A31 = "a31";
    public static final String A32 = "a32";
    public static final String A33 = "a33";
    public static final String A34 = "a34";
    public static final String A35 = "a35";
    public static final String A36 = "a36";
    public static final String A37 = "a37";
    public static final String A38 = "a38";
    public static final String A39 = "a39";
    public static final String A40 = "a40";
    public static final String A41 = "a41";
    public static final String A42 = "a42";
    public static final String A43 = "a43";
    public static final String A44 = "a44";
    public static final String A45 = "a45";
    public static final String A46 = "a46";
    public static final String A47 = "a47";
    public static final String A48 = "a48";
    public static final String A49 = "a49";
    public static final String A50 = "a50";
4

2 回答 2

0
private static final String[] elems;


static
{
    elems = new String[table_elements];
    for(int i = 0; i < table_elements;i++)
    {
         elems[i] = "a"+i;
    }
}
于 2013-06-20T01:46:47.243 回答
0

两件事情。

1)

您可以在静态块中实例化它们,但您将无法通过成员属性名称访问它们。IE FOO,BAR,ETC

static  
{  
     for(int i = 0; i< NUM_ELEMENTS;i++)  
     {  
           ELEMENTS[i] = "string"+i;
     }  
}  

2)

如果你有数百个元素,这真的很糟糕。此时您需要认真重新考虑您的架构。

于 2013-06-20T01:56:00.023 回答