I'll give an example to explain what i need, imagine a Lamborghini database:
我举一个例子来解释我需要什么,想象一下兰博基尼数据库:
CarSeries (int identifier, String name)
CarModel (int modelIdentifier, String modelName, CarSeries (int identifier, String name);
Now I want to create an array where I should be able to directly create a carModel
, for example a carModel (123, Gallardo, 345, Superleggera)
现在我想创建一个数组,我应该可以直接创建一个carModel,例如carModel(123,Gallardo,345,Superleggera)
So I create the array for three cars:
所以我为三辆车创建阵列:
CarModel carmodels[] = new CarModel[3];
CarModel carmodels [] =新CarModel [3];
And have to initialize them. Initializing a similar array 0 position for CarSeries is as simple as
并且必须初始化它们。为CarSeries初始化类似的数组0位置非常简单
carseries[0] = new CarSeries(0,"");
carseries [0] = new CarSeries(0,“”);
but I can't find the way to initialize a CarModel, and neither to use getters and setters for, for example, set carModel carSeries's identifier.
但是我找不到初始化CarModel的方法,也没有使用getter和setter来设置carModel carSeries的标识符。
Update: Based in RAZ_Muh_Taz's code (very helpful, thanks!) I've been able to initialize it! This way: carmodels[0] = new CarModel (0,"", new CarSeries(0,"")); Now i want to set a console output asking for CarSeries identifier, how should code ask for input? How can I use the getter now?
更新:基于RAZ_Muh_Taz的代码(非常有帮助,谢谢!)我已经能够初始化它!这样:carmodels [0] = new CarModel(0,“”,new CarSeries(0,“”));现在我想设置一个控制台输出,询问CarSeries标识符,代码应该如何请求输入?我现在该如何使用吸气剂?
Update2: Finally just created a new array for each Person and just update both arrays simultaneously with same count to point both desired groups of data.
Update2:最后刚刚为每个Person创建了一个新数组,只需同时更新两个数组,并指向所需的数据组。
6 个解决方案
#1
1
Once you have declared the carmodel array, you have to access to the index of the array to insert each one car models.
一旦声明了carmodel数组,就必须访问数组的索引以插入每个汽车模型。
CarModel carModels[] = new CarModel[3];
carModels[0] = new CarModel(0, "model1", new CarSeries(0, "serie1"));
carModels[1] = new CarModel(1, "model2", new CarSeries(1, "serie2"));
carModels[2] = new CarModel(2, "model3", new CarSeries(2, "serie3"));
You can also declare the CarSeries before
您也可以在之前声明CarSeries
CarSeries carSerie1 = new CarSeries(0, "serie1");
CarSeries carSerie2 = new CarSeries(1, "serie2");
CarSeries carSerie3 = new CarSeries(2, "serie3");
and use the reference in the constructor of the CarModel
并在CarModel的构造函数中使用引用
carModels[0] = new CarModel(0, "model1", carSerie1);
carModels[1] = new CarModel(1, "model2", carSerie2);
carModels[2] = new CarModel(2, "model3", carSerie3);
You have to take into account that you have to declare the constructors properly or the code won´t compile
您必须考虑到必须正确声明构造函数或代码不能编译
#2
1
Your third parameter is a CarSeries
, so just pass that in.
你的第三个参数是CarSeries,所以只需传入。
CarSeries accord = new CarSeries (0, "accord");
CarModel honda = new CarModel(0, "honda", accord);
Or in-line.
CarModel honda = new CarModel(0, "Honda", new CarSeries (0, "Accord"));
Whether that is in an array or not is not a problem, though, I think you should ideally store some collection of series for each model, not only one.
然而,无论是否在数组中都不是问题,我认为理想情况下应该为每个模型存储一些系列集合,而不仅仅是一个。
#3
1
You need to create a new object when passing it to your parameter in your CarModel contructor.
在将其传递给CarModel构造函数中的参数时,需要创建一个新对象。
CarModel carmodels[] = new CarModel[3];
carseries[0] = CarModel (0, "Lexus", new CarSeries (0, "RX350"));
carseries[1] = CarModel (1, "Toyota", new CarSeries (1, "Tacoma"));
carseries[2] = CarModel (2, "Honda", new CarSeries (2, "CR-V"));
#4
1
You can do it the following way provided you have the appropriate constructors for CarModel and CarSeries defined.
只要您为CarModel和CarSeries定义了适当的构造函数,就可以通过以下方式执行此操作。
CarModel carmodels[] = new CarModel[3] {
new CarModel(1, "gallardo", new CarSeries(100, "xyz")),
new CarModel(2, "veneno", new CarSeries(200, "abc"),
new CarModel(3, "Huracan", new CarSeries(300, "pqr")
};
#5
0
You'd bether to use Map interface.
你可以使用Map界面。
Map<Object, Object>yourMapName = new HashMap<Object, Object>();
But I recommend to you to read much more for collections and their hierarchy.
但我建议您阅读更多关于集合及其层次结构的内容。
#6
-2
First of all you cannot instantiate your model like that>
What you are doing is: instantiate CarModel
with name carmodels[]
首先,你不能像那样实例化你的模型>你正在做的是:实例化名为carmodels []的CarModel
You can do a arraylist
你可以做一个arraylist
#1
1
Once you have declared the carmodel array, you have to access to the index of the array to insert each one car models.
一旦声明了carmodel数组,就必须访问数组的索引以插入每个汽车模型。
CarModel carModels[] = new CarModel[3];
carModels[0] = new CarModel(0, "model1", new CarSeries(0, "serie1"));
carModels[1] = new CarModel(1, "model2", new CarSeries(1, "serie2"));
carModels[2] = new CarModel(2, "model3", new CarSeries(2, "serie3"));
You can also declare the CarSeries before
您也可以在之前声明CarSeries
CarSeries carSerie1 = new CarSeries(0, "serie1");
CarSeries carSerie2 = new CarSeries(1, "serie2");
CarSeries carSerie3 = new CarSeries(2, "serie3");
and use the reference in the constructor of the CarModel
并在CarModel的构造函数中使用引用
carModels[0] = new CarModel(0, "model1", carSerie1);
carModels[1] = new CarModel(1, "model2", carSerie2);
carModels[2] = new CarModel(2, "model3", carSerie3);
You have to take into account that you have to declare the constructors properly or the code won´t compile
您必须考虑到必须正确声明构造函数或代码不能编译
#2
1
Your third parameter is a CarSeries
, so just pass that in.
你的第三个参数是CarSeries,所以只需传入。
CarSeries accord = new CarSeries (0, "accord");
CarModel honda = new CarModel(0, "honda", accord);
Or in-line.
CarModel honda = new CarModel(0, "Honda", new CarSeries (0, "Accord"));
Whether that is in an array or not is not a problem, though, I think you should ideally store some collection of series for each model, not only one.
然而,无论是否在数组中都不是问题,我认为理想情况下应该为每个模型存储一些系列集合,而不仅仅是一个。
#3
1
You need to create a new object when passing it to your parameter in your CarModel contructor.
在将其传递给CarModel构造函数中的参数时,需要创建一个新对象。
CarModel carmodels[] = new CarModel[3];
carseries[0] = CarModel (0, "Lexus", new CarSeries (0, "RX350"));
carseries[1] = CarModel (1, "Toyota", new CarSeries (1, "Tacoma"));
carseries[2] = CarModel (2, "Honda", new CarSeries (2, "CR-V"));
#4
1
You can do it the following way provided you have the appropriate constructors for CarModel and CarSeries defined.
只要您为CarModel和CarSeries定义了适当的构造函数,就可以通过以下方式执行此操作。
CarModel carmodels[] = new CarModel[3] {
new CarModel(1, "gallardo", new CarSeries(100, "xyz")),
new CarModel(2, "veneno", new CarSeries(200, "abc"),
new CarModel(3, "Huracan", new CarSeries(300, "pqr")
};
#5
0
You'd bether to use Map interface.
你可以使用Map界面。
Map<Object, Object>yourMapName = new HashMap<Object, Object>();
But I recommend to you to read much more for collections and their hierarchy.
但我建议您阅读更多关于集合及其层次结构的内容。
#6
-2
First of all you cannot instantiate your model like that>
What you are doing is: instantiate CarModel
with name carmodels[]
首先,你不能像那样实例化你的模型>你正在做的是:实例化名为carmodels []的CarModel
You can do a arraylist
你可以做一个arraylist