C/C++;Java中的结构类比?
我有下面的C代码,我对Java知之甚少 我想知道是否有任何方法可以在Java中创建如下代码所示的结构。我想我们可以在Java中使用C/C++;Java中的结构类比?,java,c++,c,struct,Java,C++,C,Struct,我有下面的C代码,我对Java知之甚少 我想知道是否有任何方法可以在Java中创建如下代码所示的结构。我想我们可以在Java中使用class,但我在Java类中面临的问题是我不能声明人[10],即这样一个结构的数组 struct people{ float height; float weight; int age; }people[10]; int main() // this part of code is just to show how I can access all t
class
,但我在Java类中面临的问题是我不能声明人[10],即这样一个结构的数组
struct people{
float height;
float weight;
int age;
}people[10];
int main() // this part of code is just to show how I can access all those elements of struct
{
int i;
for(i=0;i<10;i++)
{
people[i].height = rand()%7;
people[i].weight = rand()%80;
people[i].age = rand()%100;
}
for(i=0;i<10;i++)
{
printf(" %f %f %d\n",people[i].height,people[i].weight,people[i].age);
}
return 0;
}
struct-people{
浮动高度;
浮重;
智力年龄;
}人民[10];
int main()//这部分代码只是为了说明如何访问struct的所有这些元素
{
int i;
对于(i=0;i公共类人员{
私人浮动高度;
私人浮重;
私人互联网;
公众人士(浮子高度、浮子重量、智力)
{
高度=高度;
重量=重量;
这个。年龄=年龄;
}
公共浮点getHeight(){return height;}
...
...
公共静态void main(字符串[]args)
{
int N=10;
人员[]人员=新人员[N];
对于(inti=0;i您应该使用类。对于创建人,您必须创建该类的对象
people [] p = new people[10];
for(people person : p )
{
//each object is defined.
p = new People(h,w,age);
}
class People{
People(int h, int w, int a)
{
height=h; weight=w; age=a;
}
float height;
float weight;
int age;
}
在C++中,可以静态分配对象
struct People
{
// struct members are public by default
float height;
float weight;
int age;
}
people[10]; // array of 10 objects
int main ()
{
// fill some data
people[0].age = 15;
people[0].height = 1.60;
people[0].weight = 65;
return 0;
}
但是在Java中,您必须动态分配对象,创建数组不会分配对象,它只会分配一个引用数组
package Example;
private class People
{
// define members as public
public float height;
public float weight;
public int age;
}
class Main
{
public static main (String [] args)
{
// array of 10 references
People [] p = new People [10];
// allocate an object to be referenced by each reference in the array
for (int i=0; i<10; i++)
{
p[i] = new People();
}
// fill some data
people[0].age = 15;
people[0].height = 1.60;
people[0].weight = 65;
}
}
包示例;
私人阶层人士
{
//将成员定义为公共成员
公众浮标高度;
公众浮力;
公共信息;
}
班长
{
公共静态main(字符串[]args)
{
//10个引用的数组
人[]p=新人[10];
//为数组中的每个引用分配一个要引用的对象
对于(int i=0;i,我将这样做:
public class Person {
public float height;
public float weight;
public int age;
private static Person[] people = new Person[10];
public static void main(String[] args) {
java.util.Random r = new java.util.Random();
for (int i = 0; i < 10; ++i) {
people[i] = new Person();
people[i].height = r.nextInt(7);
people[i].weight = r.nextInt(80);
people[i].age = r.nextInt(100);
}
for(int i = 0; i < 10; ++i) {
System.out.printf(" %f %f %d\n",
people[i].height, people[i].weight, people[i].age);
}
}
}
公共类人物{
公众浮标高度;
公众浮力;
公共信息;
私人静态人员[]人=新人员[10];
公共静态void main(字符串[]args){
java.util.Random r=new java.util.Random();
对于(int i=0;i<10;++i){
人[我]=新人();
人[i].身高=r.nextInt(7);
人[i].体重=r.nextInt(80);
人[i].年龄=r.nextInt(100);
}
对于(int i=0;i<10;++i){
System.out.printf(“%f%f%d\n”,
人[i]。身高,人[i]。体重,人[i]。年龄);
}
}
}
为什么不呢?类数组在Java中完全可以。为什么不可以呢?我说过我对Java知之甚少,那么为什么不投票呢?@Ravi,因为你应该在提问之前研究过这个问题(这在FAQ-中),你的问题很基本,很明显你没有。OP有公共成员,没有用户定义的构造函数,所以你的例子和他们的完全不同。谢谢@Khaled清楚地解释了这一点。谢谢你,先生,介绍了我的代码的同上版本,尽管这只是我和回答者的一个例子。我已经做了这个“Person[]people=new Person[10]“我一直缺少的唯一一件事就是‘people[I]=new Person();”……谢谢你和Khaled再次指出这一点。
public class Person {
public float height;
public float weight;
public int age;
private static Person[] people = new Person[10];
public static void main(String[] args) {
java.util.Random r = new java.util.Random();
for (int i = 0; i < 10; ++i) {
people[i] = new Person();
people[i].height = r.nextInt(7);
people[i].weight = r.nextInt(80);
people[i].age = r.nextInt(100);
}
for(int i = 0; i < 10; ++i) {
System.out.printf(" %f %f %d\n",
people[i].height, people[i].weight, people[i].age);
}
}
}