0

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.

Community
  • 1
  • 1
  • Few comments: **1st** Patient.patientNo refers to classe's static data (common for all objects) and I bet you want every Patient to be unique. **2nd** ***public static void main(String[] args)*** is a method not a class – Artur Nov 07 '13 at 10:58
  • the link you've pasted contains accepted answer that is not that correct as author thought ;-) – Artur Nov 07 '13 at 11:27

6 Answers6

4

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);
}
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
3

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);
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • You should use a List instead of an array. In Java avoid using arrays, Lists are far better. – m0skit0 Nov 07 '13 at 10:56
  • @Skippy-psI'mawoman - Well, the array solution might work, but I recommend using a List, as its dynamic and thus you needn't worry about the size. – Rahul Nov 07 '13 at 10:58
  • In that case, I suggest you take out 30-60 mins of your time and just go through the internet about arrays and array lists. These days, generally the norm is ArrayList. Arrays are used very less in comparison. – Rahul Nov 07 '13 at 11:08
  • I'm the wrong person to ask this question. You need to ask this to yourself. The answer which best helped you solve the problem, should be the one you accept. – Rahul Nov 07 '13 at 11:44
2

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
    }
}
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
2

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;
    }
}
Siddhartha Gupta
  • 507
  • 3
  • 11
2

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);
    }
}
lkamal
  • 3,788
  • 1
  • 20
  • 34
2

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

Artur
  • 7,038
  • 2
  • 25
  • 39