Connecting Tech Pros Worldwide Help | Site Map

Can someone help me create an array of objects??

Newbie
 
Join Date: Apr 2007
Posts: 22
#1: Apr 27 '07
Hi,
I've created a class called Person which has parameters "First_name, Surname, month (of birth), day (of birth)". This class creates person objects fine but now i want to create another class which will take these objects and store them in an array. As i dont know how many Person objects i will create i can't make a predefined array size.

Any help would be very grateful,

Thanks
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Apr 27 '07

re: Can someone help me create an array of objects??


Store them in a List<Person> instead. Read the API docs of the ArrayList
and LinkedList classes; they're both implementations of the List interface.

kind regards,

Jos
Newbie
 
Join Date: Apr 2007
Posts: 22
#3: Apr 27 '07

re: Can someone help me create an array of objects??


Thanks for that, I have now created an arraylist called people, but i have come across a problem. This is my code :

import java.util.*;
import java.util.ArrayList.*;

public class People
{

public static void main (String[] args)
{
ArrayList<Person> people = new ArrayList<Person>();
people.add( Person.First_name );
people.add( Person.Surname );


}
}

When I try to compile the code it tells me i cant reference the non-static variable First_name into a static context. I know i'm not allowed to do this but i cant see how to over come it. I cant change either of my methods from static to non-static and visa versa. Any ideas??
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Apr 27 '07

re: Can someone help me create an array of objects??


Do you want me to delete this thread because your other thread is basically
a duplicate of this one and the other thread gives you all the information needed.

kind regards,

Jos
Reply