user3956566
user3956566

Reputation:

Adding objects to an array

I have been looking at questions of how to add elements to an array How can I dynamically add items to a Java array?.

I do not understand how to add objects of a class type, not a datatype like String. How am I supposed to do this, when the object patient has various datatypes? What I can't get my head around, is how to put the attributes of a Patient object into an array.

Class Patient{

    public Patient(String ptNo, String ptName, int procType) throws IOException
    {
        Patient.patientNo =  ptNo;
        Patient.patientName = ptName;
        Patient.procedureType = procType;
    }
}

Another class:

public static void main(String[] args) throws IOException
    {
        Patient [] patients;
        Patient p = new Patient(null, null, 0);

        int i = 0;
        for (i = 0; i < 2; i++)
        {
        patients.add(p);
        }
    }

I understand I am missing the obvious, and only come here, after exhausting other resources.

Upvotes: 0

Views: 11606

Answers (6)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

You need to specify the array size like below

Patient [] patients = new Patient[2];

Then now add the elements like below

patients[i] = new Patient(null, null, 0)

The complete code like below

for (int i = 0; i < 2; i++)
{
  patients[i] = new Patient(null, null, 0);
}

Upvotes: 4

Artur
Artur

Reputation: 7257

First:

Some fixes:

class Patient
{

    public String patientNo;
    public String patientName;
    public int procedureType;

    public Patient(String ptNo, String ptName, int procType)
    {
        this.patientNo =  ptNo;
        this.patientName = ptName;
        this.procedureType = procType;
    }
}

Since you want your patients to be unique not the same as Patient.sth is class' property (common to all instances).

Now array and inserting:

Patient patients[] = new Patient[2];

for (int i = 0; i < patients.length; i++)
{
    patients[i] = new Patient(null, null, 0);
}

but again do not fill array with the same objects. In addition not to be bound to fixed array size - use Vector (for example)

Update: about class members / aka static object members

These 2 code samples are completely different things:

class X
{
    public String abc;

    public void someMember()
    {
        this.abc =  ;
    }
}

and

class X
{

    public static String abc;

    public void someMember()
    {
        X.abc = 
    }
}

You must distinguish between what is unique to an abject and what is common to a class (ie available to all instances - common to them). With static keyword you declare class members (they will be common foa all instances). You cannot use them with 'this' keyword which is reserved for instance members from the first example.

Here is what you must read class vs instance members

Upvotes: 2

lkamal
lkamal

Reputation: 3938

First you need to initialize an array to be of a specific size.

Patient [] patients = new Patient[2];

Secondly, you need to use index when assigning a value.

patients[i] = p;

Finally;

public static void main(String[] args) throws IOException
{
    Patient [] patients = new Patient[2];

    for (int i = 0; i < patients.length; i++)
    {
        patients[i] = new Patient(null, null, 0);
    }
}

Upvotes: 2

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

You are using an array and not an ArrayList thus add them by specifying the index

    for (i = 0; i < 2; i++)
    {
    patients[i] = p;
    }

EDIT

If you really want to assign the same object in the string you can even skip the object reference, like

public static void main(String[] args) throws IOException
{
    Patient [] patients = new Patient[2];

    int i = 0;

    for (i = 0; i < 2; i++)
    {
        patients[i] = new Patient(null, null, 0); // No need for p now
    }
}

Upvotes: 2

Siddhartha Gupta
Siddhartha Gupta

Reputation: 507

Arrays are accessed via index: Please modify your code like this.

public static void main(String[] args) throws IOException
{
    Patient [] patients = new Patient[10];
    Patient p = new Patient(null, null, 0);

    int i = 0;
    for (i = 0; i < 2; i++)
    {
      patients[i] = p;
    }
}

Upvotes: 2

Rahul
Rahul

Reputation: 45060

You need to access an array using the indexes

patients[i] = p;

but before that you need to initialize it as well.

Patient [] patients = new Patient[10]; // random init

Since you want them to be dynamic, try to use an ArrayList and keep adding the objects to it.

List<Patient> patients = new ArrayList<>();
patients.add(p);

Upvotes: 3

Related Questions