0

In C I would create a data structure as below:

struct file_data_format
{
  char name[8][20];
  float amp[8];
  int filter[8];
};

extern struct file_data_format f_data;

Then I could read or write this whole structure to a file or memory location.

How would I do this in a class in java?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

4 Answers4

2

You should read basics of Java before asking. Structure in C can be written as Class in Java.

public class FileDataFormat implements Serializable {

   String[][] name = new String[8][20];
   float[] amp = new float[8];
   int[] filter = new int[8];

   public FileDataFormat() {

   }

   public void setName(String[][] name) {
      this.name = name;
   }

   public String[][] getName() {
      return this.name;
   }

   // next getters and setters
}

I pretty recommend OOP(encapsulation, polymorphism, inheritance).

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • You should probably keep the array notations on those variables, given that an array of `float`s is not the same as a single `float` (same for `int`). The `name` 2D array in his code would need to be an array of 8 `String`s in the Java class. Unless OP really doesn't want arrays. – ajp15243 Mar 19 '13 at 20:43
  • @ajp15243 anwer updated. – Simon Dorociak Mar 19 '13 at 20:50
1

If you want to achieve a similar effect, you can do the following.

Unfortunately, you don't have so much control over how it's represented in memory as you do in c

public class file_data_format
{
  public char name[8][20];
  public float amp[8];
  public int filter[8];
}

...

public static void main()
{
    file_data_format fdf = new file_data_format();
    fdf.name = charArrayIGotFromSomewhere
}
  • +1 one since this is the only one that really mimics the ease of struct field access. For the OP's benefit, I want to stress that breaking encapsulation like this is considered a deadly sin in the OOP realm -- private fields with public getters/setters are preferred, despite the boilerplate. – ach Mar 19 '13 at 20:48
  • 1
    With IDE's the boilerplate can be filled with 3-4 mouse clicks. People need to know this. – Sotirios Delimanolis Mar 19 '13 at 20:53
1
public class FileDataFormat {
    private String name;
    private float amp;
    private int filter;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getAmp() {
        return amp;
    }

    public void setAmp(float amp) {
        this.amp = amp;
    }

    public int getFilter() {
        return filter;
    }

    public void setFilter(int filter) {
        this.filter = filter;
    }

}
ach
  • 6,164
  • 1
  • 25
  • 28
David Mathias
  • 296
  • 3
  • 12
1

The equivalent of a struct in Java is a JavaBean, as other answers has shown you.

From Wikipedia, a JavaBean :

  • is serializable
  • has a 0-argument constructor
  • allows access to properties using getter and setter methods.

To write and read it from file or memory, it is not as simple as in C. You would typically use Java Object serialization to write/read your objects to an ObjectInputStream/ObjectOutputStream, that could be attached to a file or a byte array.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58