简体   繁体   English

Java(适用于Android)在类数组(类似C的结构)中定义和初始化类数组

[英]Java (for Android) Define and Initialize class array inside class array (c-like struct)

I am rewriting c code to Java to run on an Android-based BeagleBone Black. 我正在将C代码重写为Java以在基于Android的BeagleBone Black上运行。 Since there are not struc types in java, I'm defining the data structures using public static class methods. 由于Java中没有struc类型,因此我使用公共静态类方法定义数据结构。 However, in the structures are an array of sub-structures. 但是,结构中是子结构的数组。 As a model, say we want to define a class called Satellites. 作为模型,假设我们要定义一个名为Satellites的类。 In each Satellite are Transponders and for each transponder is the channel information. 在每个卫星中都有应答器,对于每个应答器都是信道信息。 I am trying to figure out how to declare and then initialize the sub-classes inside the class. 我试图弄清楚如何在类中声明然后初始化子类。 My basic definition is: 我的基本定义是:

public static final int SATELLITE_MAX = 50;
public static final int TRANSPONDER_MAX = 24;
public static final int CHANNEL_MAX = 36;

public static class satellite_struct {
    public string satId;
    public string satName;
    public string satLocation;

    public class transponder_struct {
        public int frequency;
        public char polarity;

        public class channels_struct {
            public string channel_name;
            public string encryption;
            public int sid;
        }
    }
}

I can instantiate the satellite structure class as follows: 我可以实例化卫星结构类,如下所示:

 satellite_struct satellites[] = new satellite_struct[SATELLITE_MAX]

but, how do I initialize the x transponders and y channels inside this class? 但是,如何在此类中初始化x应答器和y通道?

Thanks. 谢谢。

The "easiest" solution would be: “最简单”的解决方案是:

public static class satellite_struct {
    public string satId;
    public string satName;
    public string satLocation;

    public class transponder_struct {
        public int frequency;
        public char polarity;

        public class channels_struct {
            public string channel_name;
            public string encryption;
            public int sid;
        }

        public channels_struct[] channels = new channel_struct[ CHANNEL_MAX ];
    }
    public transponder_struct transponders = new transponder_struct[ TRANSPONDER_MAX ];
}

Given that you defined 鉴于您已定义

satellite_struct satellites[] = new satellite_struct[SATELLITE_MAX];

You can then access them by (eg): 然后,您可以通过以下方式访问它们:

satellites[x].transponders[y].channels[z].channel_name

However, you'd probably want to Java-fy it a bit: 但是,您可能想对Java进行一下分析:

public static class Satellite {
    public string satId;
    public string satName;
    public string satLocation;

    public class Transponder {
        public int frequency;
        public char polarity;

        public class Channel {
            public string channel_name;
            public string encryption;
            public int sid;
        }

        public Channel[] channels = new Channel[ CHANNEL_MAX ];
    }
    public Transponder transponders = new Transponder[ TRANSPONDER_MAX ];
}

Class names are customarily upper-case in Java. 在Java中,类名通常是大写的。 If this is a direct translation to an existing structure, the above should do, but you might want to use a List or SparseArray instead of arrays. 如果这是对现有结构的直接转换,则应该执行上述操作,但是您可能希望使用ListSparseArray而不是数组。

Declare transponders here;
Declare channels here;

public static class satellite_struct {
    public string satId;
    public string satName;
    public string satLocation;
    public []transponder t;
    public []channel c;
    public satellite_struct(int x,int y)
    {
       //here
       t=new transponder[x];
       for(....)
           t[i]=new transponder(params);
       c=new channel[y];
       for(....)
           c[i]=new channel(params);
    }

} }

I got it to work as follows, and Java-ifying my syntax. 我按如下方式工作,并通过Java修改了我的语法。 The issue was with where to place the [] declaring an array of objects: 问题在于将[]声明对象数组的位置:

public static class Satellite {
    public String satId;
    public String satName;
    public String satLocation;

    public static class Transponder {
        public int frequency;
        public char polarity;

        public static class Channel {
            public String channel_name;
            public String encryption;
            public int sid;
        }
        public Channel channels[] = new Channel[CHANNELS_MAX];
    }
    public Transponder transponders[] = new Transponder[TRANSPONDERS_MAX];
}

Then, in my main code: 然后,在我的主要代码中:

Structures.Satellite satellites[] = new Structures.Satellite[SATELLITES_MAX];

satellites[0].transponders[0].channels[0].channel_name="HELLO";

Update: It appears that while the IDE allows me to 'see' the transponders and channels when coding, when the code runs it returns error "all elements are null" for these sub-array values. 更新:看来,虽然IDE允许我在编码时“查看”应答器和通道,但在代码运行时,对于这些子数组值,它返回错误“所有元素均为空”。 How to initialize these? 如何初始化这些?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM