用一行初始化一个 Java 数组
我正在进行从 C++ 到 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,
};
感谢
I am doing a conversion from C++ to Java.
Does anyone know how to convert it to 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,
};
Thank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要
RuleList
的构造函数:这假设
_NULL
是定义的静态int
值,而_LAMBDA
是定义的静态String
值。You'll need a constructor for
RuleList
:This assumes that
_NULL
is a defined staticint
value and_LAMBDA
is a defined staticString
value.在 Java 中执行此操作的标准方法如下。 Java 中的 RuleList 类比 RuleList C 结构更详细一些,但是有一些简单的方法可以处理这个问题,例如使用 Eclipse 生成大部分代码。
The standard way to do this in Java is below. The RuleList class in Java is a bit more verbose than the RuleList C struct, but there are easy ways of dealing with that such as using Eclipse to generate most of the code.
您可以创建一个类 RuleList,然后创建该类的一个数组:
在上面的代码中,您将看到与 C++ 不同的东西,因为在 Java 中,几乎所有东西都是对象。所以你声明step1a_rules是一个包含其他对象的数组(
Rulelist
)。所以,这个类的每个对象,都必须使用关键字new
来实例化新对象。@编辑:啊,我差点忘了:你应该注意到,java和C++有一些不同:在C++类中,默认成员是私有的。在Java类中,默认成员是public。当然,你上面的代码没有问题,因为结构体的默认成员也是公共的:D
You can create a class RuleList, after that create an array of this class:
In above code, you will see something different from C++ because with Java, almost everything is object. so you declare step1a_rules is an array contain other objects (
Rulelist
). So, each object of this class, you must use keywordnew
to instance new object.@ edit : Ah, and I almost forget: you should notice that, java and C++ has some different: in C++ class, default member is private. and in Java class, default member is public. Of course, your above code is no problem, because default member of struct is public too :D