C# ArrayList calling on a constructor class
I'm aware that an ArrayList is probably not the way to go with this particular situation, but humor me and help me lose this headache.
I have a constructor class like follows:
class Peoples
{
public string LastName;
public string FirstName;
public Peoples(string lastName, string firstName)
{
LastName = lastName;
FirstName = firstName;
}
}
And I'm trying to build an ArrayList to build a collection by calling on this constructor. However, I can't seem to find a way to build the ArrayList properly when I use this constructor. I have figured it out with an Array, but not an ArrayList.
I have been messing with this to try to build my ArrayList:
ArrayList people = new ArrayList();
people[0] = new Peoples("Bar", "Foo");
people[1] = new Peoples("Quirk", "Baz");
people[2] = new Peopls("Get", "Gad");
My indexing is apparently out of range according to the excepti开发者_开发知识库on I get.
It should be:
people.Add(new Peoples(etc.));
instead of
people[0] = new people()...;
Or better yet:
List<People> people = new List<People>();
people.Add(new People);
Just to be complete. Using a straight array:
People[] people = new People[3];
people[0] = new People();
Try people.Add(new Peoples("Bar", "Foo");
You should add elements to the list. Like the following
ArrayList people = new ArrayList();
people.Add(new Peoples("Bar", "Foo"));
You need to do
people.Add (new Peoples("Bar", "Foo"));
people.Add (new Peoples("Quirk", "Baz"));
people.Add (new Peoples("Get", "Gad"));
When you attempt to call people[i] without first populating the array list you will get the IndexOutOfRangeException. You must first add to the ArrayList.
ArrayList list = new ArrayList();
list.Add(new Peoples("Bar", "Foo"));
You can then access the list by the index which would be done in a foreach or for loop.
Is there a reason you are not using List<Peoples>
which would give you a strongly typed collection?
Also, you have publicly accessible fields in the class although I realise you probably just threw together that code for the question.
You should use the ArrayList.Add function to add to the array list.
ArrayList peoplesArray = new ArrayList();
peoplesArray.Add(new Peoples("John","Smith");
FYI, ArrayList is considered evil by many. As Kevin said, it would be better to us List<People>.
List is what is called a generic. Google 'strongly typed', 'boxing', and 'generics' for a better understanding of why.
back to your original question: An array's size must be declared when instantiated, i.e. People[] people = new People[5];
this creates 5 empty cells in the array so you can access the cells using a subscript i.e. [0]
An ArrayList or List<T> when instantiated using the default constructor has no cells i.e. List<People> people = new List<People>();
people[0] does not exist at this point.
use people.Add(new People("first", "last")); to add a new cell to the list. now the subscript [0] is valid, but [1] is still invalid because there is only one cell.
A list i.e. ArrayList or List can grow dynamically by using .Add(). Once added to a list, you can reference them using the subscript [i], but you cannot use the subcript to add them.
精彩评论