0

I am rewriting c code to Java to run on an Android-based BeagleBone Black. Since there are not struc types in java, I'm defining the data structures using public static class methods. However, in the structures are an array of sub-structures. As a model, say we want to define a class called 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?

Thanks.

1
  • I added an example to my answer -- welcome to StackOverflow. If you have questions, rather than editing the answer, you should add a comment to ask. Commented Jan 7, 2015 at 19:33

3 Answers 3

1

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 (e.g.):

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

However, you'd probably want to Java-fy it a bit:

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. 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.

Sign up to request clarification or add additional context in comments.

Comments

0
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);
    }

}

Comments

0

I got it to work as follows, and Java-ifying my syntax. 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. How to initialize these?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.