How to define a struct array in Java?
In Java, there are no built-in struct types, but you can use classes to simulate the functionality of struct arrays.
For example, we can define a class called Person to represent a structure with two properties, name and age.
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Then you can create an array of type Person to store multiple Person objects.
Person[] people = new Person[3];
people[0] = new Person("Alice", 25);
people[1] = new Person("Bob", 30);
people[2] = new Person("Cathy", 35);
This defines a structure array containing three Person objects. Each Person object in the array can be accessed using people[i].