2012-02-10 91 views
0

我正在从C++转换为Java。 有谁知道如何将其转换为Java?在一行中初始化Java数组

typedef struct { 
     int id;     
     char *old_end;   
     char *new_end;   
     int old_offset;  
     int new_offset;  
     int min_root_size;  
    int func;   
     } RuleList; 

static RuleList step1a_rules[] = 
     { 
     101, "sses",  "ss", 3, 1, -1, _NULL, 
     102, "ies",  "i",  2, 0, -1, _NULL, 
     103, "ss",  "ss", 1, 1, -1, _NULL, 
     104, "s",   _LAMBDA, 0, -1, -1, _NULL, 
     000, NULL,  NULL, 0, 0, 0, _NULL, 
     }; 

感谢

+2

*“in one line”*当源代码的主要目的是人类可读时,这是一个非常强制的限制。顺便说一句 - 请不要忘记在问题中增加一个问号,并且它会支付一些(或任何)努力来解决问题。 – 2012-02-10 03:52:36

+0

如果数组的类型是RuleList,那么无论如何,您必须使用RuleList对象初始化它。你不能只在定义中放置文字。您还需要创建一个Java类来反映您的结构。 – 2012-02-10 03:54:01

+0

你在这里错过了一大串大括号;确切地说,每一行都是如此。 – Xeo 2012-02-10 04:00:52

回答

1

你需要一个构造RuleList

class RuleList { 
    int id;     
    String old_end;   
    String new_end;   
    int old_offset;  
    int new_offset;  
    int min_root_size;  
    int func; 

    RuleList(int id, String old_end, String new_end, int old_offset, 
     int new_offset, int min_root_size, int func) 
    { 
     this.id = id; 
     this.old_end = old_end; 
     // etc. 
    } 
}; 

static RuleList[] step1a_rules = { 
    new RuleList(101, "sses",  "ss", 3, 1, -1, _NULL), 
    new RuleList(102, "ies",  "i",  2, 0, -1, _NULL), 
    new RuleList(103, "ss",  "ss", 1, 1, -1, _NULL), 
    new RuleList(104, "s",   _LAMBDA, 0, -1, -1, _NULL), 
    new RuleList(000, null,  null, 0, 0, 0, _NULL), 
}; 

这假定_NULL是一个定义的静态int值和_LAMBDA是一个定义的静态String值。

+1

我不会在Java中的变量名中使用下划线。 另外,下面这行不会编译,因为你缺少“新RuleList []”: static RuleList [] step1a_rules = { – 2012-02-10 04:05:29

+0

我其实认为在最近的Java版本中,你可以省略'new RuleList []' '{'...?或者,也许这只适用于例如整数。 – 2012-02-10 04:26:43

+0

@Ben - 我只是重复使用OP的变量名称。我同意他们不是通常的Java风格(尽管他们在语言中是合法的)。最后一条语句是完全合法的:它是一个不需要'new RuleList []'的数组初始值设定项。 (请参见[Java语言规范,第10.6节](http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.6))。 – 2012-02-10 04:32:00

1

在Java中执行此操作的标准方式如下。 Java中的RuleList类比RuleList C结构更加冗长,但是有很多简单的方法可以处理,比如使用Eclipse生成大部分代码。

public class RuleList { 
    private final int id;     
    private final String oldEnd;   
    private final String newEnd;   
    private final int oldOffset;  
    private final int newOffset;  
    private final int minRootDize;  
    private final int func; 

    public RuleList(int id, String oldEnd, String newEnd, int oldOffset, 
     int newOffset, int minRootDize, int func) { 
    this.id = id; 
    this.oldEnd = oldEnd; 
    this.newEnd = newEnd; 
    this.oldOffset = oldOffset; 
    this.newOffset = newOffset; 
    this.minRootDize = minRootDize; 
    this.func = func; 
    } 

    public int getId() { 
    return id; 
    } 
    public String getOldEnd() { 
    return oldEnd; 
    } 
    public String getNewEnd() { 
    return newEnd; 
    } 
    public int getOldOffset() { 
    return oldOffset; 
    } 
    public int getNewOffset() { 
    return newOffset; 
    } 
    public int getMinRootDize() { 
    return minRootDize; 
    } 
    public int getFunc() { 
    return func; 
    } 
} 

RuleList[] step1aRules = new RuleList[] { 
    new RuleList(101, "sses",  "ss", 3, 1, -1, 0), 
    new RuleList(102, "ies",  "i",  2, 0, -1, 0), 
    new RuleList(103, "ss",  "ss", 1, 1, -1, 0), 
    new RuleList(104, "s",   _LAMBDA, 0, -1, -1, 0), 
    new RuleList(000, null,  null, 0, 0, 0, 0), 
}; 
+2

你能告诉我你为什么使用final作为变量吗?这意味着它是否与上面的C++代码相关?谢谢。 – hqt 2012-02-10 04:13:53

+1

这意味着你不打算在构造你的RuleList之后改变变量的值。如果你想能够改变这个值,那么你应该删除最后的并添加一个setter,如下所示:public void setId(int id){this。id = id; } – 2012-02-10 04:20:21

+1

的'新RuleList []'是在最后声明中不必要的。另外,由于成员变量被声明为“final”,所以可以公开它们并免除访问函数。 – 2012-02-10 04:33:25

0

您可以创建一个类RuleList,在这之后创建这个类的一个数组:

public class RuleList{ 
    int id ;   
    char old_end; //note that java does not use (* for pointer like C++)  
    char new_end;   
    int old_offset;  
} 

static RuleList[] step1a_rules = new RuleList[]{ 
        new RuleList (101,"sses","ss",3, 1,-1, 0),... }; 

在上面的代码中,你将看到不同的东西,从C++,因为与Java,几乎一切都是对象。所以你声明step1a_rules是一个包含其他对象的数组(Rulelist)。所以,这个类的每个对象,都必须使用关键字new来实例化新对象。我几乎忘记了:你应该注意到,java和C++有一些不同:在C++类中,默认成员是私有的。而在Java类中,默认成员是公共的。当然,上面的代码没有问题,因为struct的默认成员也是公共的:D

+0

在Java类中,默认访问是_package private_:只能在同一个包中定义的代码中访问,而且最后一条语句中不需要'new RuleList []'你是否尝试过?你需要显式声明接受参数的'RuleList'构造函数。 – 2012-02-10 04:34:30