如何在Java中从类外部向类中的对象数组添加对象
我有以下代码:如何在Java中从类外部向类中的对象数组添加对象,java,arrays,class,Java,Arrays,Class,我有以下代码: class Person{ private Car [] cars; public Person(){ //other variables... cars = new Car[10]; } //other methods..... //... //... public void setCar(Car car, int position){ this.cars[position] = car; }
class Person{
private Car [] cars;
public Person(){
//other variables...
cars = new Car[10];
}
//other methods.....
//...
//...
public void setCar(Car car, int position){
this.cars[position] = car;
}
public Car getCar(int position){
return cars[position];
}
}
我的Car
类没有问题,问题是当我将新Car
分配给数组时
我是这样分配的:
public class MainClass{
public static void main(String [] args){
Car newCar = new Car();
Person newPerson = new Person();
int position = 0;
int x= 0;
int option;
do{
//Code for menu options and user input detection of options...
//...
//...
switch(option){
case 1:
// get user input and set it to newCar variables.....
newCar.setVariableX();
newCar.setVariableY();
//assigment of new car to array of cars in newPerson
newPerson.setCar(newCar,position);
position++;
break;
case 2: x =2; //exit option
break;
}while(x!=2);
// invoque print option for Array of Cars
}
}
因此,对数组的赋值似乎是正确的,只添加了一个Car
,然后打印出ok,但是如果我添加了多个Car
,它会将新的Car
变量赋值给数组的所有位置。例如:
Add car1 to cars[position].
position++
Add car2 to cars[position].
我得到的结果是
cars[0] = car2;
cars[1] = car2;
找到了问题,谢谢各位:
public class MainClass{
public static void main(String [] args){
Car newCar; //<-----------changed this
Person newPerson = new Person();
int position = 0;
int x= 0;
int option;
do{
//Code for menu options and user input detection of options...
//...
//...
switch(option){
case 1:
newCar = new Car(); //<------------This fixed the issue
// get user input and set it to newCar variables.....
newCar.setVariableX();
newCar.setVariableY();
//assigment of new car to array of cars in newPerson
newPerson.setCar(newCar,position);
position++;
break;
case 2: x =2; //exit option
break;
}while(x!=2);
// invoque print option for Array of Cars
}
}
public类MainClass{
公共静态void main(字符串[]args){
Car newCar;//您应该有:
newCar = new Car();
newCar.setVariableX();
newCar.setVariableY();
您总是添加相同的引用,因此当您修改对象时,数组中的所有对象都会被修改。您应该有以下内容:
newCar = new Car();
newCar.setVariableX();
newCar.setVariableY();
您总是添加相同的引用,因此当您修改对象时,数组中的所有对象都会被修改。将您的Car newCar=newCar()
语句移动到do while
循环中,它应该会按照您的预期工作。移动您的Car newCar=new Car()
语句在您的中执行while
循环,它应该可以像您期望的那样工作。您可以使用ArrayList存储多个对象,并在需要时添加/删除。这样您就不局限于10辆车
class Person{
private ArrayList<Car> cars;
public Person(){
cars = new ArrayList<Car>();
}
//other methods.....
//...
//...
public void setCar(Car car, int position){
this.cars[position] = car;
}
public Car getCar(int position){
return cars[position];
}
public void addCar(Car car){
cars.add(car);
}
}
班级人员{
私家车;
公众人士(){
cars=新阵列列表();
}
//其他方法。。。。。
//...
//...
公共车辆(车辆,内部位置){
这个。汽车[位置]=汽车;
}
公共汽车getCar(内部位置){
返回车辆[位置];
}
公共车辆{
cars.add(car);
}
}
此外,您当前的问题是您不断添加同一个Car对象,因为newCar从未被重新分配。您可以使用ArrayList存储多个对象,并在需要时添加/删除。这样您就不限于10辆车
class Person{
private ArrayList<Car> cars;
public Person(){
cars = new ArrayList<Car>();
}
//other methods.....
//...
//...
public void setCar(Car car, int position){
this.cars[position] = car;
}
public Car getCar(int position){
return cars[position];
}
public void addCar(Car car){
cars.add(car);
}
}
班级人员{
私家车;
公众人士(){
cars=新阵列列表();
}
//其他方法。。。。。
//...
//...
公共车辆(车辆,内部位置){
这个。汽车[位置]=汽车;
}
公共汽车getCar(内部位置){
返回车辆[位置];
}
公共车辆{
cars.add(car);
}
}
另外,您当前的问题是,您不断添加相同的Car对象,因为newCar从未被重新分配。我不认为这会导致您看到的问题,但我注意到您没有以中断来结束案例。这会给您带来一些麻烦。是的,我的代码中确实有中断,只是忘了在此处添加它们我的帖子。你总是在添加同一辆车。你如何添加新车?你每次添加一辆车时都需要一个新的语句。你的代码并不完整。这只是一个复制问题的最小但完整的示例。我不知道它如何会导致你看到的问题,但我注意到你没有以中断结束你的案例给你带来一些麻烦。是的,我的代码中确实有中断,只是忘了在我的帖子中添加它们。你总是在添加同一辆车。你如何添加新车每次添加一辆车时都需要一个新的语句。你的代码不完整。这是一个复制问题的最小但完整的示例。你能访问带方括号的ArrayList吗?你能吗访问带有方括号的ArrayList?