How do you initialize a 1-dimensional array when you don't know the size in C #?

I tried very hard to initialize a 1-dimensional array without knowing its size, but I do not get it; Please tell me how I can declare an array whose size will dynamically increase in accordance with the requirement in C #.

I mean, I want to do something like this

class A
{
    int[] myarray;
    int i=0;
    while(i<5)
    {
        myarray[i]==n;
        n=n%10;
        i++;
    }
}

Please, help!!!

+3
source share
6 answers

The user can also declare this array type using ARRAYLIST.

The System.Collections namespace defines a class known as ArrayList that can store an array of objects with dynamic size.

   ArrayList cities = new ArrayList();
   cities.Add("Pune");
   cities.Add("Mumbai");
0
source

In C you will need to work with Malloc and Realloc, in C # you should prefer a list.

Class A {
   List<int> integerList = new List<int>();

   void DoSometing() {   
   for(int i = 0; i < somewhat; i++) {
         integerList.Add(i);
      }
   }
}
+5

++:

 #include <vector>

 // ...

 std::vector<int> myVector;  // will resize as you 'push_back'
+1

:

C: malloc() realloc()

++: std::vector

#: List

+1

C malloc .
++ std::vector <T> .
# List <T> .

.. C ++ #, .

0

...

int IAmTheSize = 183475; // just a random int. Replace it with your value.
int[] myArray = new int[IAmTheSize];

. , , List<>.

List<int> myList;

int i=0;
while(i < 5)
{
    myList.Add(n);
    n = n % 10;
    i++;
}

, , , . BTW, , =, myarray[i]=n;, myarray[i]==n; ( bool/boolean/logical).


List<>You can read more about the class here: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

0
source

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


All Articles