C # ArrayList calling constructor class

I know that ArrayList is probably not a way to go with this particular situation, but laugh at me and help me lose this headache.

I have a constructor class, for example:

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 assemble the collection by calling this constructor. However, I cannot find a way to build an ArrayList correctly when I use this constructor. I figured this out with an array, but not an ArrayList.

I already bothered 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 seems to be out of range with the exception that I get.

+4
source share
7 answers

It should be:

 people.Add(new Peoples(etc.)); 

instead

 people[0] = new people()...; 

Or even better:

 List<People> people = new List<People>(); people.Add(new People); 

Just to be complete. Using a direct array:

 People[] people = new People[3]; people[0] = new People(); 
+9
source

Try people.Add(new Peoples("Bar", "Foo");

+4
source

You must add items to the list. As below

 ArrayList people = new ArrayList(); people.Add(new Peoples("Bar", "Foo")); 
+3
source

You need to do

 people.Add (new Peoples("Bar", "Foo")); people.Add (new Peoples("Quirk", "Baz")); people.Add (new Peoples("Get", "Gad")); 
+3
source

When you try to call people [i] without first populating the array list, you will get an IndexOutOfRangeException. You must add to ArrayList first.

 ArrayList list = new ArrayList(); list.Add(new Peoples("Bar", "Foo")); 

You can then access the list by index, which will be executed in a foreach or for loop.

Is there a reason you are not using List<Peoples> , which will give you a strongly typed collection? Also, you have public fields in the class, although I understand that you probably just compiled this code for the question.

+3
source

You should use the ArrayList.Add function to add arrays to the list.

 ArrayList peoplesArray = new ArrayList(); peoplesArray.Add(new Peoples("John","Smith"); 
+2
source

FYI, ArrayList is considered by many to be evil. As Kevin said, we would be better off List <People>.

A list is what is called a general. Google has “scored”, “boxing” and “generics” to better understand why.

back to the original question: The size of the array must be declared when creating the instance, i.e. People [] people = new people [5];

this creates 5 empty cells in the array so you can access the cells using the index, that is [0]

ArrayList or List <T> when an instance using the default constructor has no cells, i.e. List <People> people = new List <People> ();

people [0] do not exist at this point.

use people.Add (new people ("first", "last")); to add a new cell to the list. now index [0] is valid, but [1] is still invalid because there is only one cell.

List i.e. ArrayList or List can be dynamically incremented with .Add (). After adding to the list, you can reference them using the index [i], but you cannot use a substring to add them.

0
source

Source: https://habr.com/ru/post/1308148/


All Articles