基本上我想創建一個在編譯時已知的值的數據結構。在C我會做這樣的:如何在java中初始化一個靜態的對象數組
struct linetype { int id; char *descr; };
static struct linetype mylist[] = {
{ 1, "first" },
{ 2, "second" }
};
我已經在Java中發現的唯一soultion涉及在運行時創建數組:
public class Outer {
public class LineType {
int id;
String descr;
private LineType(int a, String b) {
this.id = a;
this.descr = b;
}
}
LineType[] myList = {
new LineType(1, "first"),
new LineType(2, "second"),
};
當結構得到長這看似繁瑣和低效(和複雜)。有另一種方法嗎? (注意:請忽略任何語法錯誤,因爲這只是爲這個問題創建的示例代碼,另外,我知道String是指向數據段的字符指針以外的其他字符,然而,該參數適用於基本數據類型也是如此)。
都能跟得上推遲靜態初始化,沒有。 –
很多編碼暴行都是以效率的名義提交:) – dasblinkenlight