用一行初始化一个 Java 数组

发布于 2025-01-04 01:11:36 字数 660 浏览 0 评论 0原文

我正在进行从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘然心甜 2025-01-11 01:11:36

您需要 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值。

You'll need a constructor for 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),
};

This assumes that _NULL is a defined static int value and _LAMBDA is a defined static String value.

孤独陪着我 2025-01-11 01:11:36

在 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),
 };

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.

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),
 };
白况 2025-01-11 01:11:36

您可以创建一个类 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类中,默认成员是public。当然,你上面的代码没有问题,因为结构体的默认成员也是公共的:D

You can create a class RuleList, after that create an array of this class:

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),... };

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 keyword new 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文