Dynamic array definition

How to define a dynamic array in C #?

+3
source share
7 answers

C # does not provide dynamic arrays. Instead, it offers a List class that works the same way.

To use lists, write at the top of the file:

using System.Collections.Generic;

And where you want to use the list, write (example for strings):

List<string> mylist = new List<string>();
mylist.Add("First string in list");
+15
source

Take a look at Array.Resize if you need to resize the array.

    // Create and initialize a new string array.
    String[] myArr = {"The", "quick", "brown", "fox", "jumps", 
        "over", "the", "lazy", "dog"};

    // Resize the array to a bigger size (five elements larger).
    Array.Resize(ref myArr, myArr.Length + 5);

    // Resize the array to a smaller size (four elements).
    Array.Resize(ref myArr, 4);

List, . , , , . . .

    List<string> dinosaurs = new List<string>(4);

    Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

    dinosaurs.Add("Tyrannosaurus");
    dinosaurs.Add("Amargasaurus");
    dinosaurs.Add("Mamenchisaurus");
    dinosaurs.Add("Deinonychus");

, ToArray() .

    string[] dinos = dinosaurs.ToArray();
+8

# . . (https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx). , , .

class Program
{
    static void Main(string[] args)
    {
        int x = Convert.ToInt32(Console.ReadLine());
        int y = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(x);
        {


            int[] dynamicArray1 = { };//empty array
            int[] numbers;//another way to declare a variable array as all arrays start as variable size
            numbers = new int[x];//setting this array to an unknown variable (will be user input)

            for (int tmpInt = 0; tmpInt < x; tmpInt++)//build up the first variable array (numbers)
            {
                numbers[tmpInt] = tmpInt;
            }
            Array.Resize(ref numbers,y);// resize to variable input
            dynamicArray1 = numbers;//set the empty set array to the numbers array size 
            for (int z = 0; z < y; z++)//print to the new resize
            {
                Console.WriteLine(numbers[z].ToString());//print the numbers value
                Console.WriteLine(dynamicArray1[z].ToString());//print the empty set value
            }
        }
        Console.Write("Dynamic Arrays  ");
        var name = Console.ReadLine();

    }
}
+1

In fact, you can have dynamic arrays in C # very simple. keep in mind that the answer to your question above is also correct, you can declare a General List The way to create a dynamic array was to declare your array, for example

        string[] dynamicArry1 = { };//notice I did not declare a size for the array
        List<String> tmpList = new List<string>();
        int i = 1;
        for(int tmpInt = 0; tmpInt < 5; tmpInt++)
        {
           tmpList.Add("Testing, 1.0." + tmpInt + ", 200, 3.4" + tmpInt +"," + DateTime.Now.ToShortDateString());
           //dynamicArry1[tmpInt] = new string[] { tmpList[tmpInt].ToCharArray() };
        }
        dynamicArry1 = tmpList.ToArray();
0
source

how about an arraylist?

If I'm not mistaken, ArrayList is an implementation of dynamic arrays

0
source

An example of defining a dynamic array in C #:

        Console.WriteLine("Define Array Size? ");
        int number = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter numbers:\n");
        int[] arr = new int[number];

        for (int i = 0; i < number; i++)
        {
            arr[i] = Convert.ToInt32(Console.ReadLine());
        }

        for (int i = 0; i < arr.Length; i++ )
        {
            Console.WriteLine("Array Index: "+i + " AND Array Item: " + arr[i].ToString());
        }

        Console.ReadKey();  
0
source

So

int nSize = 17;
int[] arrn = new int[nSize];

nSize++;
arrn = new int[nSize];
-1
source

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


All Articles