更改 ADT 数组中一项中的字段的问题
Problem with changing a field in one item in an ADT array
我正在尝试编辑 Person[] 数组中的一项,但由于某种原因,每当调用 setHasVirus 时,整个数组都会更改为“有病毒”。从字面上看,每个项目的 hasVirus 变量都从 false 更改为 true。我试图只编辑第二个项目,但它不起作用。你能告诉我有什么问题吗?
class Person{
boolean vaccine;
boolean hasVirus;
boolean alive;
}
class arrayADT{
Person[] people;
int count;
}
public static String inputString(String message)
{
Scanner scanner = new Scanner(System.in);
System.out.println(message);
return scanner.nextLine();
}
public static int inputInt(String message)
{
return Integer.parseInt(inputString(message));
}
public static int inputIntRange(String message, int min, int max)
{
int result = Integer.parseInt(inputString(message));
while((result < min) | (result > max))
{
result = Integer.parseInt(inputString(message));
}
return result;
}
public static Person setVaccine(Person a, boolean b){ //accessor method
a.vaccine = b;
return a;
}
public static Person setHasVirus(Person a, boolean b){ //accessor method
a.hasVirus = b;
return a;
}
public static Person setAlive(Person a, boolean b){ //accessor method
a.alive = b;
return a;
}
public static Person createPatient(boolean a, boolean b, boolean c){ //initialises the object
Person d = new Person();
d = setVaccine(d, a);
d = setHasVirus(d, b);
d = setAlive(d, c);
return d;
}
public static Person getSingleRecord(arrayADT ap, int number)
{
return ap.people[number];
}
public static arrayADT addPatientRecord(arrayADT ap, Person a) //adds one record to the existing array adt
{
ap.people[ap.count] = a;
ap.count = ap.count + 1;
return ap;
}
public static arrayADT inputPeople(){
arrayADT a = new arrayADT();
int howMany = inputIntRange("Number of people to simulate?(between 100 and 1000)", 100, 1000);
a.people = new Person[howMany];
Person d = createPatient(false, false, true);
for(int i = 0; i<howMany; i++){
addPatientRecord(a, d); //filling out the array with default values
}
setHasVirus(getSingleRecord(a, 2),true); //trying to edit only one item in the whole array
return a;
}
这是因为您只创建了 Person
的 1 个实例,并用指向这个实例的指针填充了整个数组。
为了修复您描述的行为,您需要为每个数组条目创建一个单独的实例:
// ...
for(int i = 0; i<howMany; i++) {
Person p = createPatient(false, false, true); //filling out the array with default values
addPatientRecord(a, p);
}
// ...
我正在尝试编辑 Person[] 数组中的一项,但由于某种原因,每当调用 setHasVirus 时,整个数组都会更改为“有病毒”。从字面上看,每个项目的 hasVirus 变量都从 false 更改为 true。我试图只编辑第二个项目,但它不起作用。你能告诉我有什么问题吗?
class Person{
boolean vaccine;
boolean hasVirus;
boolean alive;
}
class arrayADT{
Person[] people;
int count;
}
public static String inputString(String message)
{
Scanner scanner = new Scanner(System.in);
System.out.println(message);
return scanner.nextLine();
}
public static int inputInt(String message)
{
return Integer.parseInt(inputString(message));
}
public static int inputIntRange(String message, int min, int max)
{
int result = Integer.parseInt(inputString(message));
while((result < min) | (result > max))
{
result = Integer.parseInt(inputString(message));
}
return result;
}
public static Person setVaccine(Person a, boolean b){ //accessor method
a.vaccine = b;
return a;
}
public static Person setHasVirus(Person a, boolean b){ //accessor method
a.hasVirus = b;
return a;
}
public static Person setAlive(Person a, boolean b){ //accessor method
a.alive = b;
return a;
}
public static Person createPatient(boolean a, boolean b, boolean c){ //initialises the object
Person d = new Person();
d = setVaccine(d, a);
d = setHasVirus(d, b);
d = setAlive(d, c);
return d;
}
public static Person getSingleRecord(arrayADT ap, int number)
{
return ap.people[number];
}
public static arrayADT addPatientRecord(arrayADT ap, Person a) //adds one record to the existing array adt
{
ap.people[ap.count] = a;
ap.count = ap.count + 1;
return ap;
}
public static arrayADT inputPeople(){
arrayADT a = new arrayADT();
int howMany = inputIntRange("Number of people to simulate?(between 100 and 1000)", 100, 1000);
a.people = new Person[howMany];
Person d = createPatient(false, false, true);
for(int i = 0; i<howMany; i++){
addPatientRecord(a, d); //filling out the array with default values
}
setHasVirus(getSingleRecord(a, 2),true); //trying to edit only one item in the whole array
return a;
}
这是因为您只创建了 Person
的 1 个实例,并用指向这个实例的指针填充了整个数组。
为了修复您描述的行为,您需要为每个数组条目创建一个单独的实例:
// ...
for(int i = 0; i<howMany; i++) {
Person p = createPatient(false, false, true); //filling out the array with default values
addPatientRecord(a, p);
}
// ...