One, array
Can hold any number of data of the same type
1. Declaration and assignment of data
Merge writing: data type [] Variable name = new data type [length]
① declaration: Data type [] variable name;
② Assignment: Variable name = new data type [length]
2. Read and modify array items
Read: variable name [index]
(from the specified array, the value of an array item is fetched by number, the return type is the same as the value of the array item type)
Modify: Variable name [index] = value;
Example:
int [] numbers = new int[3];
Numbers[0] = 3;
Numbers[1] =-2;
Numbers[3] = numbers[0] * 2 + numbers[1];
Console.WriteLine (numbers[3]);
Index range 0-4
3. Read the length of the array
code format: variable name. The length
returns the type int
Example:
creates a string array of length 3, and then sequentially assigns each of its items a value of a,b,c. Finally, the length of the array is output.
string[] STRs = new String[3];
Strs[0] = "a";
Strs[1] = "B";
Strs[2] = "C";
Console.Write (STRs. Length);
Simplified to:
string[] STRs = {"A", "B", "C"};
Console.Write (STRs. Length);
Console.WriteLine (Numbers. Length);
4. Array initializer
array initializer refers to assigning values to an array at the same time, specifying the value of each item of the array
Code ①:
int[] nums;//declaring an array
Nums = new int[3]{2, 5, 1};//Using an array initializer, Assigns a value to an array while specifying the value of each item
Use an array initializer to assign a value to an array and specify its value for each item
string[] STRs = new string[2]{"Hello", "World"};
Code ②:
Does not specify an array length, the compiler automatically populates the array length based on the number of initializers
int[] Nums1 = new int[]{2,3};//The compiler sets the length of the array to 2
int[] Nums2 = new int[]{2,3,1};//The compiler sets the length of the array to 3
Simplified to:
Int[] nums1 = {2,3};
Int[] Nums2 = {2,3,1};
Second, the fixed staying power of the array
When an array is created, its length is constant
Arrays are suitable for scenarios where the number of data is fixed:
1. Suitable for scenarios where arrays are used:
① Save all prime numbers within 100
② preserving data for all known planets in the solar system
③ Save all card data in standard poker
④ Save all dates for one weeks
⑤ Other fixed-length data scenarios
2. Scenarios that are not suitable for use with arrays:
① save a class of student information
② Save all dates of the year
③ Save a player's hand data in a bucket landlord game
④ save a player's equipment information in the game
⑤ Other indeterminate data scenarios
Example:
int[] numbers = new INT[3];
Numbers[0] = 3;
Numbers = new Int[2];
Console.WriteLine (Numbers[0]);
Result is 0
Three, the traversal of the array
Example:
With a data variable named arrays, output the value of each item in the array.
Console.WriteLine (Arrays[i]); Variation range of variable i: 0-arrays. Length-1
for (int i = 0; I <=arrays. Length-1; i++)
{
Console.WriteLine (arrays);
}
Is the start of the first item in the array, and all the items of the array are taken at once
To implement the traversal of an array, you can use the loop
The loop variable starts at 0 and then takes the maximum subscript of the array (the length of the array-1)
In the loop body, the value of each item of the array can be removed by using the loop variable as the subscript
for (int i = 0; i < arrays. Length; i++)
{
Console.WriteLine (Arrays[i]);
}
Iv. sort of exchange
Sorting problem: There is an array that holds a number of numbers, and now requires that the numbers in the array be arranged in order from small to large
Example:
Analysis: If the digital peso with index 0 is a large number with a J, then swap. J =1-arrays. Length-1
for (int j = 0; J < arrays. Length-1; J + +)
{
for (int j = i+1; J < arrays. Length; J + +)
{
if (Arrays[0] >arrays[j])
{
int temp = arrays[0];
Arrays[0] = arrays[J];
arrays[j] = temp;
}
}
}
Example:
#region Creating an array
Console.Write ("Please enter the length of the array:");
int len = Int. Parse (Console.ReadLine ());
int[] numbers = new Int[len];
for (int i = 0; i < numbers. Length; i++)
{
Console.Write ("Please enter the array" + (i + 1) + "item:");
Numbers[i] = Int. Parse (Console.ReadLine ());
}
#endregion
Console.clear ();
#region Ascending sort
for (int i = 0; i < numbers. Length-1; i++)
{
for (int j = i + 1; j < numbers. Length; J + +)
{
if (Numbers[i] > Numbers[j])
{
int temp = Numbers[i];
Numbers[i] = Numbers[j];
NUMBERS[J] = temp;
}
}
}
#endregion
#region Output Array
Console.WriteLine ("The number you entered is sorted as follows:");
for (int i = 0; i < numbers. Length; i++)
{
Console.Write (Numbers[i] + "");
}
Console.WriteLine ();
#endregion
#region Looking for odd numbers
Console.WriteLine ("Where the following numbers are odd:");
for (int i = 0; i < numbers. Length; i++)
{
If (Numbers[i]% 2! = 0)
{
Console.Write (Numbers[i] + "");
}
}
Console.WriteLine ();
#endregion
#region Looking for prime numbers
Console.WriteLine ("The following numbers are prime numbers:");
for (int i = 0; i < numbers. Length; i++)
{
BOOL Isfind = false;
for (int j = 2; J < Numbers[i]; j + +)
{
If (numbers[i]% J = = 0)
{
Isfind = true;
Break
}
}
if (!isfind)
{
Console.Write (Numbers[i] + "");
}
}
#endregion
Console.ReadLine ();
Five, set
Functionally, the array can be implemented by all functions, the collection can be implemented
Array Collection
For saving multiple data of the same type
Array
Fixed length: Used to hold a set amount of data
Low memory consumption
Fast traverse speed
Collection
Indefinite length: The amount of data saved can change continuously during the execution of the program
Memory-intensive
Slow traverse speed
Collection types supported by the C # language
List
1. Create
① Definition: List < data type > variable name;
② Assignment: Variable name = new List < data type > ();
Definition and Assignment Merge writing:
List < data type > variable name = new list< data type > ();
③ initializer: variable name = new lise< data type >{element 1, Element 2, Element 3,...... Element n};
2. Operation
① add element: variable name. Add (the element to be added)
② Insert element: Inserts a new element into the specified position of the collection
Code: Variable name. Insert (index, the data to be inserted);
Example:
list<int> nums = new list<int>{3,5,7};
Nums. Insert (1,0);
③ deleting elements
The variable name. Remove at (index);
Removes the element at the specified index location
The variable name. Remove (data);
Deletes the first occurrence of the same data that is filled in the collection
Example:
List<int>nums = new List <int>{1,1,2,3,5};
Nums. Remove at (2); Represents the deletion of an element with an index of 2
Nums. Remove (1); Deletes the first element that is the same as data 1
④ modifying elements
To modify the value of an element in a collection
Code: variable name [index] = value
Read and modify elements are exactly the same way as arrays do
⑤ getting the number of elements
Gets the length of the collection
Code: Variable name. Count
Queue
Stack
LinkedList
Hashset
Other
Six, foreach Loop
Can only be used to iterate over an array or collection
Code format:
foreach (data type variable in array or collection)
{
Loop body
}
① represents the data for each item from an array or collection, in turn
② data is assigned to the loop variable every data is fetched
③ once each assignment, run the loop body once
Example: A list collection with an int type is known, and the variable name is numbers, which requires that each item in the collection be output sequentially.
foreach (int item in numbers)
{
Console.WriteLine (item);
}
A foreach loop is also called a read-only loop
The collection or array cannot be changed in the loop body
foreach Loop:
① can only be used to traverse
② cannot change the loop target
③ fast traverse speed and high execution efficiency
For loop:
① can be used in any form of repetitive behavior
② in the loop body, can do anything
③ Slow traverse speed and low execution efficiency
The Foreach loop is most appropriate if you need to traverse a collection or array, and the traversal is only read without changes. Conversely, the other loops are selected as needed.
Arrays and collections (Basic)