1
public class Patient extends Person {
    private String Diagnosis;
    private Appointment[] appointment = new Appointment[2];
    private int numberofAppointment;
    public static int numberOfPatient;

    public void DellappointmentAT(int index) {
        appointment[index].setAvailable(true);
        numberofAppointment--;
    }
}

The class i created, has an Array of object as data fields and i have this method that should remove an element from this array, I want to delete the element without changing the array size.

2
  • what is the problem? Commented Mar 20, 2020 at 5:18
  • Normal arrays have a fixed size in Java so you are using the wrong datatype in regard of the requirements here. Use an ArrayList for this task. Commented Mar 20, 2020 at 5:26

2 Answers 2

1

Removing an element will impact the array size.

If you really need the initial array to stay the same, there are 2 common approaches:

  1. Create another array so your initial array is not modified
  2. Soft delete - create a remove flag inside the object. That way, you can differentiate between removed data and the array size stays the same
Sign up to request clarification or add additional context in comments.

Comments

0

How about this:

public void DellappointmentAT(int index) {
    appointment[index].setAvailable(true);
    numberofAppointment--;
    for (int i = index; i < numberofAppointment; ++i) {
        appointment[i] = appointment[i+1];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.