Basic programming, .NET technology.

DATA STRUCTURES - 1


Data structures and algorithms – the nightmare of most IT students. After finishing school, I, as well as my fellow students, confidently declared that: He knows us, and we don't know them. Therefore, this series of articles on "Data Structures and Algorithms" was born, the purpose is to help me review my knowledge of data structures and algorithms.

What is data structure? It is a technique for organizing and arranging data in a computer's memory. How data is sorted and organized affects how it is retrieved and used. Imagine that the memory of your computer is just your tiny attic… and the data is the items in your home, how you organize those items will affect how you find them and use them. they.

In this article, we will learn about: Arrays (Array). Shall we know what they are? Performance and how to use them?

     1. Array
A collection of data elements stored contiguously in a memory area. The elements of a must have the same data type (data type). Array elements can be accessed directly based on the index. In .Net Framework, the index starts from 0.

Consider the following example:

Statement 1: We declare an array of type integer
Statement 2: We initialize the array with 4 elements.
The allocation and initialization of memory to store the array will happen as shown below.

To work with arrays we need to know 2 types of common operations: Allocate and access.
- Allocate: this is the declaration and initialization of the array, it will allocate a memory area on the stack when we declare the array and a memory area on the heap when we initialize the size of the array.
- Access: to access any element of the array, we use index. As said .Net Framework starts from 0. With the following code, we assign the value to the 2nd element and retrieve its value to use.

The elements of the array are stored contiguously in a block of memory, and it is accessed directly through the index of the array. That's why reading and writing arrays is fast. However, the array requires a size declaration when initialized, and its elements must be of the same data type.

To decide whether to use arrays in programming or not, we need to consider three issues: do we require fast data access or do we need to be fast? Is it possible to know in advance the number of elements in a data set? And is the data type of the elements the same?

However, we can use ArrayList to replace Array in case the size of the array cannot be fixed or the datatype of the element is different.

Share:

0 nhận xét:

Đăng nhận xét

Featured Posts

Data type 3 - string type