Java: How to use an array of structures (sorry, classes)?
Im not an expert when it comes to Java, but have been writing C/C++ code for many years. So what I need to do in java is create an array of structures and be able to use it in my code.
OK, in C/C++ I would do it this way (and I will simplify it):
typedef struct mystruct{
char name[20];
int age;
};
....
in main:
mystruct People[20]; //this creates an array of 20 structs.
People[0].age = 40;
strcpy(People[0].name, "justme");
etc...
How can I do this sort of thing in Java? Some sample code for doing the above example would be nice.
This is what I tried in Java:
class myclass{
String Name;
int age;
};
...
myclass People[] = new myclass[20];
But if I try:
People[0].Name = "justme";
I get a null pointer exception. What am I doing wrong?
EDIT: Just thought I would mention that the actual structure I will be using is bigger (7 strings) and the array of these structures needs to be at least 256.