Chapter 3 Array Structure of Visual C # Best Practices (I): Array

Source: Internet
Author: User
ArticleDirectory
    • 3.1.1 array Concept
    • 3.1.2 two-dimensional array
    • 3.1.3 array Initialization
    • 3.1.4 precautions
Chapter 3 Array Structure

Storage-related data item groups are mostly software applicationsProgramThis can be achieved by using arrays. Arrays are similar in various development languages. The declaration and initialization of arrays are very convenient. In some cases, they can replace complex structures and classes. They are small in size, fast in access, and high in resource utilization, thanks to the popularity of developers, the rational use of arrays can help us get twice the result with half the effort. The following describes the concepts and usage of arrays in detail.

The focus of this chapter:

◆ Array Concept
◆ Two-dimensional array
◆ Array Initialization
◆ Precautions

Array 3.1

First, we will introduce several concepts to help us understand the concept of arrays. An array is a set of variables of the same type, such as a group of integers and a group of characters. These variables that make up an array are called the elements of an array. The number of elements is also called the length of an array and the size of an array. The size is fixed once it is created, the number of elements in the array cannot be increased or decreased.
Each array element has a number. This number is called a subscript. This subscript is also called an index. An array with n elements has an index ranging from 0 ~ N-1. The index and capacity are the characteristics of the array. They are fixed when the array is created. When using the index operation, the defined capacity cannot be exceeded to avoid the error that the array index exceeds the limit.

3.1.1 array Concept

An array is a collection of objects of the same type. Since an array can be of almost any length, it can be used to store thousands or even millions of objects, but the size must be determined when an array is created. Each item in the array is accessed by index. The index is a number indicating the storage location or slot of the object in the array. Arrays can be used to store both the reference type and the value type. Next we will introduce a variety of commonly used arrays.
An array is a set of indexed objects. One-dimensional arrays store fixed numbers linearly. The statement is as follows:
Type [] arrayname;
The elements in the array are initialized at the same time, as shown below:
Int [] array = new int [5];
When defining an array, you can specify the number of array elements in advance and define the number of elements in the array in "[]". Here we define the number of elements as 5. The number of elements can be obtained through the "length" attribute of the array name. When using an array, you can add a subscript to [] to obtain the corresponding array element. The default value of the numeric array element is zero, and the default value of the referenced element is null. However, we can initialize the value when creating the array, as shown below:
Int [] array1 = new int [] {1, 3, 5, 7, 9 };
Or even initialize it like this:
Int [] array2 = {1, 3, 5, 7, 9 };
The index of the array starts from zero, so the index of the first element in the array is 0. Let's look at an example:
1 using system;
2 namespace Microsoft. Example
3 {
4 class testarray
5 {
6 static void main (string [] ARGs)
7 {
8 string [] days = {"sun", "mon", "Tue", "wed", "thr", "fri", "sat "}; // define an array
9 system. Console. writeline (days [0]); // The output index value is 0.
10}
11}
12}
AboveCode In, row 8th defines an array and uses data to initialize the array. As we have mentioned before, when an array is initialized, its index starts from scratch. to read data with zero index, you must use 9th lines of code, use [0] to locate the data.
Output result: Sun
Another detail is that the foreach statement is usually used to access each element stored in the array, as shown below:
Int [] numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0 };
Foreach (int I in numbers)
{
System. Console. Write ("{0}", I );
}
Output result: 4 5 6 1 2 3-2-1 0
Note that the size of an array is not part of its type, but is part of the array type in C. This allows us to declare an array and assign it any array, regardless of the length of the array, as shown below:
Int [] numbers; // defines an int array of any size.
Numbers = new int [10]; // numbers is an array with 10 elements.
Numbers = new int [20]; // now, number is an array with 20 Elements

3.1.2 two-dimensional array

In life, many things cannot be implemented using a one-dimensional array. For example, a team includes many queues, each of which has many people. A one-dimensional array can only represent one column and cannot accommodate one of our teams. To solve this problem, we can use the multi-dimensional array in the C # language. A variant of a multi-dimensional array is an array consisting of an array. In terms of concept, two-dimensional arrays are similar to grids, while three-dimensional arrays are similar Cube Body. This article focuses on introducing two-dimensional arrays:
We can define a two-dimensional array as follows:
Int [,] array2d = new int [2, 3];
In the above definition, we declare a variable named array2d two-dimensional array, and also declare its capacity, which can accommodate two lines of teams, each line can stand for three people.
You can also initialize the value when creating an array, as shown below:
Int [,] array2d2 = {1, 2, 3}, {4, 5, 6 }};
During the development process, we often need to read the values in the array. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 class testreadarray
05 {
06 static void main (string [] ARGs)
07 {
08 int [,] array2d ={{ 1, 2, 3}, {4, 5, 6 }}; // defines a two-dimensional array
09 for (INT I = 0; I <2; I ++) // traverse each row
10 {
11 For (Int J = 0; j <3; j ++) // traverse each column
12 {
13 system. Console. Write (array2d [I, j]); // read the elements in a two-dimensional array
14}
15 system. Console. writeline ();
16}
17}
18}
19}
In the above Code, row 8th defines a two-dimensional array array2d and initializes it. Then, we use the for loop to traverse the elements in the array. Here we need to use two loops. The first loop is used to traverse the number of rows, and the second loop is used to traverse the number of columns in a row. Finally, the value of the array element is output based on the index.
The final result is:
123
456
Similarly, it is as convenient as modifying arrays, as shown below:
For (INT I = 0; I <2; I ++)
{
For (Int J = 0; j <3; j ++)
{
Array2d [I, j] = (I + 1) * (J + 1); // modify the elements in a two-dimensional array
}
}

3.1.3 array Initialization

When using the C # array, you will often encounter the C # array initialization problem, but you cannot understand various initialization operations. Here we will introduce the solution to the C # array initialization problem.
1. The curly braces of int [] intarray = new int [3] {2, 3, 4} are called array initializes. The array initializes can only be used when declaring array variables, the C # array cannot be used for initialization after the array is declared, and the compiler automatically calculates the size of the array without specifying the int [] intarray = new int [] {2, 3, 4, the C # compiler also has a simpler form of int [] intarray = {2, 3, 4 };
2. Use the C # syntax in the background to create a new class derived from the basic array. In this way, you can use the array class to define the methods and attributes for each C # array. For example, the Length attribute can also use the foreach statement to iterate the array. In fact, this uses the getenumerator () method in the array class, or implements the enumerator in the array class.
3. the array class is an abstract class. You cannot use constructors to create arrays. In addition to using the C # syntax to create arrays, you can also use its createinstance () static method to create arrays, if you do not know the element type in advance or want to create an array whose index is not based on 0, you can use the static method.
4. arrays are reference types. Therefore, if an array is assigned to another array variable, two variables pointing to the same array will be obtained. The icloneable interface is implemented by copying arrays. The clone () method defined by this interface creates a shortest copy of the array. That is, if the array element is of the value type, all values are copied. If the array contains the reference type, instead of copying the element itself, it just copies the reference. To include a deep copy of an array of the reference type, you must iterate the array and create a new object.
5. array class implements the Bubble Sorting of elements in the array. The sort () method requires the implementation of the icomparable interface by elements in the array. Simple type. For example, string and int32 implement the icomparable interface (in ascending order). The call syntax is array. Sort ().

3.1.4 precautions

C # arrays work in a similar way as most popular languages, but there are still some differences that should be noted. When declaring an array, square brackets ([]) must follow the type, not the identifier. In C #, placing square brackets after identifiers is invalid syntax.
int [] Table; // valid
int table []; // invalid
the operations on the array are generally:
value operation: given a set of subscripts, read the corresponding data elements.
value assignment operation: store or modify the corresponding data elements for a group of subscripts.
clear operation: clear all data elements in the array.
copy operation: Assign the data elements of an array to another array.
sort operation: sort the data elements in the array, this requires that the data elements in the array can be sorted;
reverse operation: reverse the order of the data elements in the array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.