Data storage.

Suppose you have developed a computer game called "User Hostile" in which players compete against convoluted and unfriendly computer interface. Now you need to write a program that tracks the monthly sales of this game over a five year period. Or let's say you need to inventory Hacker Hero Trading Cards.
Very soon you will come to the conclusion that you need more than simple basic data types to store and process information.

 

Arrays. Introduction.

To make it easier to work with large amounts of data, a group of cells is given a common name. Such a group of cells is called an array.
Array – it is a group of memory cells of the same type, located side by side and having a common name. Each cell in the group has a unique number.

There are three things you need to learn when working with arrays:
x allocate memory of the required size for the array;
x write data to the desired cell;
x read data from a cell.

Create an array.

When creating an array, space is allocated in memory (a certain number of cells).
1) Arrays can be created by simply enumerating elements:
int[] nums = < code>new int[] { 1, 2, 3, 5 };

int  means that all objects in the array are integers. In place of  int  there can be any other data type. For example,
string[] names = ["Vasya", "Peter", "Fedya"];

2) We can not immediately specify the values ​​of the array, but simply create an array of the length we need.
int[] nums = new int[4];
3) An array always "knows" your size. The size of the array a can be found like this:
a.Length;
Often the size of the array is stored in a separate variable so that the program can be easily changed to work with a different array size. Example:
int N = 10; // store the size of the array in variable N
int[] nums = new int[4]; // create an array of size N
Console.Write(nums.Length); // display the size of the array
The size of the array can be set from the keyboard.

Referring to an array element.< /u>

Most of the usefulness of arrays comes from the fact that its elements can be accessed individually.
The way to do this is to use an index to number the elements.
Index is a value that points to a particular array element.

Remember: C# array numbering starts at zero.< br />
Examples of accessing the array A:
int x = (A[3] + 5) * A[1] / / read values ​​A[3] and A[1]
A[0] = x + 6 // write new value to A[0]
Let's analyze the program for working with array elements.
int i = 1;
int[] A = new int[5]; //create an array of 5 elements
A[0] = 23; // to each of the 5 elements of the array (indexes from 0 to 4)
A[1] = 12; // write a specific value
A[2] = 7;
A[3] = 43;
A[4] = 51;
A[2] = A[i] + 2*A[i-1] + A[2*i]; // change the value of the element with index 2 to the result of the expression
                                 // because i=1, then substituting the value of the variable i into the expression we get
                                // next expression A[2] = A[1] + 2*A[0] + A[2];
Console.Write((A[2] + A[4]));

As a result of executing this program, the value of the sum of the elements of the array with index 2 and with index 4 will appear on the screen, equal to 116. As you can see from the example, we can access any element of the array. And also calculate the required element number using various formulas (for example, as in the program A[i-1] or A[2*i], in these cases, the element indices will be calculated and depend on the value of i).

Let's analyze an example program.
int N = 5;
int[] A = new int[N];
int x = 1;
A[x + 4] = A[x] + A[2 * (x + 1)];  // after substituting x into expressions and calculations 
                          // get the next line A[5] = A[1] + A[4]
                          // A[5] no such element exists
                          // error - out of bounds array

The array is declared with 5 elements, which means that the elements will be numbered from 0 to 4. We see, that the program in the 6th line refers to a non-existent element: A[5].
It turns out that the program has gone beyond the bounds of the array.
An array overrun is an access to an element at an index that does not exist in the array.
In such cases, the program usually crashes with a run-time error.

 

When working with arrays, you usually have to work with all the elements of the array at once.
Iterate over elements: we look through all the elements of the array and, if necessary, perform some operation with each of them.
For this, a loop with a variable is most often used, which changes from 0 to N-1, where N  is the number of array elements.
Under N we will consider the current size of the array, that is,
N = A.Length;
...
for (int i = 0; i < N; i++)
{
     // here we work with A[i]
}
In the specified loop, the i variable will take on the values ​​0, 1, 2, ..., N-1.  Thus, at each step of the loop, we access a specific element of the array with the number i.
That is, it is enough to describe what needs to be done with one element of the A[i] array and place these actions inside such a loop.

Let's write a program that fills the array with the first natural numbers, that is, at the end of the program, the elements of the array should become equal
A[0] = 1
A[1] = 2
A[2] = 3
...
A[N - 1] = N
It's easy to see the pattern: the value of an array element must be greater by 1 than the element's index.
The loop will look like this
for (int i = 0; i < N; i++) { A[ i] = i + 1; }

Additional ways to work with array elements
Except for the for, you can also use foreach - it iterates over all elements of the array, without using indexes. Sometimes it can be convenient, for example, to display an array on the screen.   foreach(int i in A) { Console.Write(i + " "); }
And this is how you can read an array, the elements of which are written on one line and separated by a space, from the keyboard. using System.Linq; // you need to write this directive in order to use functions related to working with arrays ... string text = Console.ReadLine(); int[] array = text.Split(' ').Select(int.Parse).ToArray(); // Split() splits the string into separate objects (in this case, space division) // Select(int.Parse) converts each object (string) to an int // ToArray() writes all received objects to array