Arrays and collections

Source: Internet
Author: User
Tags array definition

5-1-Array
Array → can store any number of data of the same type

The index (subscript) of an array is a number of type int
The index (subscript) is the number of each of the array items, starting at 0,
Code Writing for arrays
Declaration and assignment of an array
Declaration: Data type [] variable name; → data type is the type of the array item
Assignment: Variable name = new data type [length]; → length is of type int, indicating the length of the array
The default value of an array item: The value of an array item after it is created, the default value for the array item type
Number type → default value is 0
bool type → default value is False
char type → default value is
String type → default value is null
Cases:
int[] numbers = new INR[5]; → Defines a numbers variable, which is an array that can hold data of 5 int types

The data type must be exactly the same
Declaration and Assignment Merge writing:
data type [] Variable name = new data type [];
Reading and modifying array items
READ: variable name [index]→ the value of an array entry from the specified array by number, the return type is the same as the array item
Modify: variable name [index] = value;
code example:
int[] Number = new int [5]; → Define an array number to hold 5 data of type int, the index range is 0-4;
Number[0] = "3"; → "3" is a string type, cannot be assigned to number[0], cannot convert string to int type; so change "3" to 3→number[0] = 3
NUMBER[5] =-2; → There is no array entry indexed to 5, so the operation error: Subscript (index) out of bounds; number[5] should be changed to number[1];
NUMBER[3] = number[0] * 2 + number[1]; →NUMBER[3] = 4;
Console.WriteLine (Number[3]); → Output result is 4
Console.WriteLine (bumbers. LENGRH); → read array length (variable name. Length) The result of the output is 5
Read array length
The variable name. length→ return type: int=
Practice:
Create a string array of length 3, and then assign a, B, C, respectively, for each of its items
Output the length of the array
string[] ZM = new String[3];
Zm[0] = "a";
ZM[1] = "B";
ZM[2] = "C";
Console.WriteLine (ZM. Length);
Shorthand:
string[] STRs = new string[3]{"A", "B", "C"}; →new string[3]{"A", "B", "C"} is an array initializer, assigning values to each of its items when creating arrays, simplifying the write to be used only in array definition statements
Console.WriteLine (ZM. Length);
Array → can store any number of data of the same type

The index (subscript) of an array is a number of type int
The index (subscript) is the number of each of the array items, starting at 0,
Code Writing for arrays
Declaration and assignment of an array
Declaration: Data type [] variable name; → data type is the type of the array item
Assignment: Variable name = new data type [length]; → length is of type int, indicating the length of the array
The default value of an array item: The value of an array item after it is created, the default value for the array item type
Number type → default value is 0
bool type → default value is False
char type → default value is
String type → default value is null
Cases:
int[] numbers = new INR[5]; → Defines a numbers variable, which is an array that can hold data of 5 int types

Data types are fully consistent
Declaration and assignment Merge writing:
data type [] Variable name = new data type [];
Reading and modifying an array item
read: variable name [index]→ the value of an array entry from the specified array, the return type is the same as the array item
Modified: variable name [index] = value;
Code example:
int[] number = new int [5];→ definition An array number holds 5 data of type int, the scope of the index is 0-4;
Number[0] = "3"; → "3" is a string type, cannot be assigned to number[0], cannot convert the string to int type; so change "3" to 3→ Number[0] = 3
Number[5] = -2;→ There are no array entries indexed to 5, so the operation error: Subscript (index) out of bounds; so the number[5] should be changed to number[1];
Number[3] = number[0] * 2 + number[1]; →NUMBER[3] = 4;
Console.WriteLine (number[3]); → The output is 4
Console.WriteLine (bumbers. LENGRH); → read array length (variable name. Length) The output is 5
Read array length
variable name. length→ return type: int=
Exercise:
//Create a string array of length 3, and then assign a, B, c
//output length for each of its entries
string[] ZM = new String[3];
Zm[0] = "a";
Zm[1] = "B";
Zm[2] = "C";
Console.WriteLine (ZM. Length);
Shorthand:
string[] STRs = new string[3]{"A", "B", "C"};→new string[3]{"a", "B", "C"} An array initializer, assigning values to each of its items when creating the arrays, Simplifying a write can only use the
Console.WriteLine (ZM) in an array definition statement. Length);
5-2-Array staying power
An array is created with a fixed length of
array for data-fixed scenarios

Scenarios that are suitable for use with arrays:
1. Save all prime numbers within 100
2. Preserving the mass of all known planets in the solar system
3. Save all dates for one weeks
Other fixed-length data scenarios
A scenario that is not suitable for using arrays:
1, save the student information of a class
2. Save all dates of the year
3, save the game of a player's equipment information
Other data scenarios with indefinite length
Once an array is created, its length is constant
Arrays are suitable for scenarios where data volume is fixed

Scenarios that are suitable for use with arrays:
1. Save all prime numbers within 100
2. Preserving the mass of all known planets in the solar system
3. Save all dates for one weeks
Other fixed-length data scenarios
A scenario that is not suitable for using arrays:
1, save the student information of a class
2. Save all dates of the year
3, save the game of a player's equipment information
Other data scenarios with indefinite length
5-3 traversal of an array
Is the start of the first item in the array, followed by all the items in the array
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
code example:
for (int i = 1; i < arrays. Length[i]); i++)
{
Console.WriteLine (Arrays[i]);
}
Is the start of the first item in the array, followed by all the items in the array
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
code example:
for (int i = 1; i < arrays. Length[i]); i++)
{
Console.WriteLine (Arrays[i]);
}
5-4-Exchange Sort
Sorting issues
There is an array in which many numbers are stored, and it is now required to arrange the arrays in the array in small to large order

Sub-Topic 2
Sorting issues
There is an array in which many numbers are stored, and it is now required to arrange the arrays in the array in small to large order

Sub-Topic 2
5-5-Application: Array Analyzer
5-6-Collection
Collection: Used to store multiple data of the same type
Indefinite length: The amount of data saved can change continuously during the execution of the program
Memory-intensive
Slow traverse speed
In the function, the array can implement all the functions, the collection can be implemented, conversely, the collection can implement some functions, the array is difficult to implement
Collection types supported by the C # language
1. List: The most common collection type
Create
Defined
lisr< data type > variable name; data type [] variable name; → Array definition
→ All represents the data type of each element (item)
Assign value
Variable name = new list< data type > (); → The set is indefinite, so the assignment is not required to specify the length, the length can be changed after the value
Array Assignment: Variable name = new data type [length]→ due to the staying power of the data, so the assignment is to specify the length, the length is fixed after the assignment.
Initialization
array initializer → variable name = newlist< data type >{element 1, Element 2, ..... Element N}
Example of creating a list combination:
list<int> nums = new list<int> (); →nums: no element; length is 0
int[] Nums = new Int[3]; →nums has elements; length is 3
The variable name. Add (the data to be added)

Operation
adding elements
Adds a new element to the end of the collection

How to write:
variable name. Add (the data to be added); → The data must be consistent with the type of elements specified in the geometry definition
code example:
list<int> nums = new list<int>{3,5,7};
Nums. ADD (1);
Nums. ADD (3);
→list<int> nums = new list<int>{3,5,7,1,3};
Insert Element
Inserts a new element
variable name into the specified position of the collection. Insert (index, data to insert);
Insert Element Example:
list<int> nums = new list<int>{3,5,7};
Nums. Insert (1,10);
→list<int> nums = new list<int>{3,10,5,7}; The
Delete element
variable name. Re oveat (index);
Deletes the element at the specified index location
variable name. Remove (data);
Delete the first occurrence of the same data in the collection as it was filled out
Example:
list<int> nums = new list<int>{1,1,2,3,5};
Nums. RemoveAt (2); → Delete the element with index 2
Nums. Remove (1); → Delete the first element that is the same as data 1
→list<int> nums = new list<int>{1,3,5};

The

Modify element
Modifies the value of an element in the collection
variable name. Index] = value;
Read and modify elements in exactly the same way as numbers
Gets the number of elements
gets the length of the collection
gets the number of elements: variable name. Count;
Gets the number length: variable name. Length;
2, Queue
3, Stack
4.Linkedlist
5, HashSet
Other
collection: for storing multiple data of the same type
indefinite length: The amount of data saved can change continuously during the execution of the program.
Memory-intensive
traversal slow
Functionally, the array can implement all the functions, the collection can be implemented, conversely, the collection can implement some functions, the array is difficult to implement
C # language supported collection types
1, List: Most common collection types
Create
Defines the
lisr< data type > variable name; data type [] variable name; → array definition
→ all represents the data type of each element (item)
Assignment
Variable name = new list< data type > (); → Set is indefinite, So the assignment is not to specify the length, the length can be changed after the value of the
Array assignment: variable name = new data type [length]→ due to the staying power of the data, so the assignment is the length must be specified, the length is fixed after the assignment
initializes the
array initializer → variable name = newlist& lt; data type >{element 1, Element 2, ..... Example of an element n}
List combination:
list<int> nums = new list<int> (); →nums: no element; length 0
int[] nums = new Int[3];→num s has elements; length is 3
variable name. Add (data to add)

Action
add element
to the end of the collection, adding a new element

How to write:
variable name. Add (the data to be added); → The data must be consistent with the type of elements specified in the geometry definition
code example:
list<int> nums = new list<int>{3,5,7};
Nums. ADD (1);
Nums. ADD (3);
→list<int> nums = new list<int>{3,5,7,1,3};
Insert Element
Inserts a new element
variable name into the specified position of the collection. Insert (index, data to insert);
Insert Element Example:
list<int> nums = new list<int>{3,5,7};
Nums. Insert (1,10);
→list<int> nums = new list<int>{3,10,5,7}; The
Delete element
variable name. Re oveat (index);
Deletes the element at the specified index location
variable name. Remove (data);
Delete the first occurrence of the same data in the collection as it was filled out
Example:
list<int> nums = new list<int>{1,1,2,3,5};
Nums. RemoveAt (2); → Delete the element with index 2
Nums. Remove (1); → Delete the first element that is the same as data 1
→list<int> nums = new list<int>{1,3,5};

modifying elements
To modify the value of an element in a collection
The variable name. [index] = value;
Read and modify elements in exactly the same way that numbers do
Get the number of elements
Gets the length of the collection
Gets the number of elements: variable name. Count;
Gets the number length: variable name. Length;
2. Queue
3. Stack
4.Linkedlist
5, HashSet
Other
5-7-foreach Loop
Forrach loops: Only arrays or collections can be traversed
Code format:
foreach (data type variable in number or collection)
{
Loop body
}

Note: From a number or collection, the data of each item is taken, and the data is assigned to the loop variable every

Sub-topic 1
code example:
A list collection of type int is known, and the variable name is Numsers, which requires that each item in the set be output sequentially
The Foreach Loop implementation:
foreach (int item in numbers)
{
Console.WriteLine (item); → High Efficiency
}

For loop implementation:
for (int i = 0; i < numbers. Length; i++)
{
Console.WriteLine (Numbers[i]); → Low Efficiency
}
code example:
A list collection of type int is known, and the variable name is Numsers, which requires that each item in the number be increased by 1
The Foreach Loop implementation:
foreach (int item in numbers)
{
item++; → Error: The Foreach loop is also called a read-only loop, and the collection or array cannot be changed in the loop body
}

For loop implementation:
for (int i = 0; i < numbers. Length; i++)
{
numbers[i]++;
}
Forach loops and for loops
foreach: can only be used for traversal, can not change the loop target, fast traverse speed, high execution efficiency
For: can be used in any form of repetitive behavior, in the loop body, can do any operation, slow traversal, inefficient execution
If you need to traverse a collection or array, and the traversal is only read without changes, use a Foreach loop, and vice versa, select other loops as needed
Forrach loops: Only arrays or collections can be traversed
Code format:
foreach (data type variable in number or collection)
{
Loop body
}

Note: From a number or collection, the data of each item is taken, and the data is assigned to the loop variable every

Sub-topic 1
code example:
A list collection of type int is known, and the variable name is Numsers, which requires that each item in the set be output sequentially
The Foreach Loop implementation:
foreach (int item in numbers)
{
Console.WriteLine (item); → High Efficiency
}

For loop implementation:
for (int i = 0; i < numbers. Length; i++)
{
Console.WriteLine (Numbers[i]); → Low Efficiency
}
code example:
A list collection of type int is known, and the variable name is Numsers, which requires that each item in the number be increased by 1
The Foreach Loop implementation:
foreach (int item in numbers)
{
item++; → Error: The Foreach loop is also called a read-only loop, and the collection or array cannot be changed in the loop body
}

For loop implementation:
for (int i = 0; i < numbers. Length; i++)
{
numbers[i]++;
}
Forach loops and for loops
foreach: can only be used for traversal, can not change the loop target, fast traverse speed, high execution efficiency
For: can be used in any form of repetitive behavior, in the loop body, can do any operation, slow traversal, inefficient execution
If you need to traverse a collection or array, and the traversal is only read without changes, use a Foreach loop, and vice versa, select other loops as needed

Arrays and collections

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.