Home > Enterprise >  Problem with changing a field in one item in an ADT array
Problem with changing a field in one item in an ADT array

Time:01-05

I am trying to edit one item in the Person[] array but for some reason, whenever the setHasVirus is called, the whole array is changed to "having the virus". Literally every single item's hasVirus variable is changed to true from false. I am trying to edit only the second item but it doesn't work. Could you please tell me what's the problem?

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;
}

CodePudding user response:

It happens because you create only 1 instance of Person and fill the entire array with the pointer to this single instance.

In order to fix the behavior you describe, you need to create a separate instance for each array entry:

// ...
  for(int i = 0; i<howMany; i  ) {
    Person p = createPatient(false, false, true); //filling out the array with default values
    addPatientRecord(a, p); 
  }
// ...
  •  Tags:  
  • Related