C # Initial Entry array

Source: Internet
Author: User
Tags array length

Arrays (data types): arrays can hold multiple data of the same type, and the array length cannot be changed.

Ⅰ, declaring and assigning values

① Array declaration: Data type [] variable name; →→ example: int[] array;//This is just a declaration. This is a group object.

② assignment: Assigns a value to the declared array →→ variable name =new data type [length];→→ example: Array=new int[5];//The array is assigned a length of 5, where [5] is also known as subscript or index.

int[]str=New int[3]{"2","4","5"}; the first kindint[]str={"3","5","9"}; second, for the simplest creation of an array.

int [] Nums=new int[6];//This just creates an array of length 6, with no assignment

Ⅱ, Array initialization

① numeric type, initialized when all values =0;②bool initialized value =false;③char =\0;④string initialization value =null;

int[] nums=New int[6];//An array is created, but no value is assigned, at which point all elements in the array are 0;//Create an array of length 3int[] numbers=New int[3];//Assign a value of 6 to an array with the array subscript 0numbers[0]=6;//re-establish a 2-length array for numbers, which is equivalent to the previous array.numbers=New int[2];//The newly created array is initialized and is not assigned, so this is 0Console.Write (numbers[0]);//at this time numbers[0]=0;

Ⅲ, array entry read and modify

① read: variable name [subscript]

② modification (also called Assignment): variable name []= value; →→array[1]=9;

Ⅳ, read data length

The variable name. Length

Note: length is an attribute of the array object.

The last value to get the array is array[array.length-1];

// Create an array int  [] array={"4","2","7" }; // i is an array subscript, array.length the array length  for  (int i=0; i<array.length;i++) {     console,write (array);
Console.WriteLine (Array[array.length-1]);}

Array fixed staying power

After an array is created, his length is fixed, and the data is applied to the number of data fixed scenes.

int [] array=newint [3];array=newint [4]; // Do you think this is not a change in length at this point? Not really.  /// just define an array of length 3 and then re-create an array of length 4 for the array, so the previous one is discarded and the length has not changed .

Array traversal

Re-assigning or summing or multiplying an array will use traversal.

int [] array={"2","4","5"};     for (int i=0; i<array.length;i++) {      console.write (array);//  Outputs an array entry after the output space is separated by Console.Write ("   ");     }

Array sorting

① Select Sort:

Take the first digit out in turn and each behind each digit comparison, bigger than he does not change position, than he is small, change position, no matter how many times to use the first to compare, with instability

1-1. Use any sort algorithm to sort the array, and then re-export the contents
var array=[6,8,5,2,2];
for (Var i=0;i<array.length-1;i++) {
for (Var j=i+1;j<array.length;j++) {
if (Array[i]>array[j]) {
var temp=array[i];
ARRAY[I]=ARRAY[J];
Array[j]=temp;
}
}
}
Console.Write (array);

② Bubble Sort

Two adjacent number to compare, who small change front, who big change back. Bubble sort has stability.

1-1. Use any sort algorithm to sort the array, and then re-export the contents
var array=[6,8,5,2,2];
I is the number of cycles
for (Var i=0;i<array.length-1;i++) {
J is the subscript of the array
for (var j=0;j<array.length-1-i; j + +) {
if (Array[j]>array[j+1]) {
var temp=array[j+1];
ARRAY[J+1]=ARRAY[J];
Array[j]=temp;
}
}
}
Console.Write (array);

Arrays and collections

Arrays: Can be fixed long, used to hold a constant amount of data, less memory, traverse fast.

Set: Indefinite length, can change length.

collection type: The List type is most common.

① Creating a Collection

Define:list< data type > variable name;

Assignment: Variable name =new List < data type > (); Example:list<int> nums=new list<int>{3,6,8};

Initialize: variable name =new list< data type >{element 1, Element 2, Element 3 ... Element n};

② Operation:

add element: variable name. Add (the data to be added);

Insert element: variable name. Insert (index, the data to be inserted);

Delete element: variable name. RemoveAt (index); Delete based on index

The variable name. Remove (data);//delete based on data

Modify element: variable name [index]= value;

Gets the number of elements: variable name. Count

"Material Problem"

1. Create an array of type int, the variable named Nums, and use the initializer to assign a value to each of its items (optionally assigning some values), and then complete the following exercise

Console.Write ("Please enter an array length:");

int len = Int. Parse (Console.ReadLine ());

Int[] Nums=new Int[len];

for (int i = 0; i < len; i++)

{

Console.Write ("array" + (i + 1) + "group data:");

Nums[i] = Int. Parse (Console.ReadLine ());

}

for (int i = 0; i < Nums. length;i++)

{

if (I < nums. LENGTH-1)

{

Console.Write (",");

}

Console.Write (Nums[i]);

}

1-1. Let the user enter a subscript and a new number, modify the array corresponding to the subscript of the number, if the subscript out of range, to give an error prompt, and let the user re-enter



Console.Write ("Please enter a subscript");

int len = Int. Parse (Console.ReadLine ());

Console.Write ("Please enter a new number");

int num = Int. Parse (Console.ReadLine ());

int[] str = {2,3,6,3,1};

if (Len <= str. LENGTH-1)

{

Str[len]=num;

Console.Write ("correct");

}

Else

{

Console.Write ("Out of range");

}

1-2. Let the user enter a number to look up to determine if the number exists in the array

int[] nums = {2,5,7,2,4};

Console.Write ("Please enter a number:");

int ter = Int. Parse (Console.ReadLine ());

BOOL Isnum = false;

for (int i = 0; i< nums. length;i++)

{

if (nums[i] = = ter)

{

Isnum = true;

Console.WriteLine ("existed");

Break

}

}

if (isnum)

{

Console.Write ("does not exist");

}

1-5. All prime numbers in the output array
Int[]nums = {2, 4, 5, 7};

Iterate through an array to get each element

for (int i = 0;i < Nums.length; i++) {

The default element is prime

Booleaniszhishu = true;

Range of prime number divisible 2~ (n-1)

for (int j= 2; j < Nums[i]; J + +) {

Determines whether composite, is composite Iszhishu is false, terminates the current loop

If (nums[i]% J = = 0) {

Iszhishu= false;

Break

}

}

Judging whether prime numbers

if (Iszhishu) {

Console.Write (nums[i]+ "is prime");

}

C # Initial Entry 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.