C# array learning

1. Definition
An array is a fixed-size sequential collection that stores elements of the same type. . An array is a collection used to store data. It is generally considered that an array is a collection of variables of the same type.
An array is a set of values ​​of the same data type.
The data is a reference type, so it is stored in the heap memory.
2. Format
Data type [] variable name = new data type [size]
Example: int[] a = new int[10]
a. Declare the array first, then initialize the array
int[] a;
int[] a = new int[10];
b. Assign the array
int[] a = {1,2,3,4,5,6} while declaring the array ;
c. Create and initialize array
int[] a = new int[5] {1,2,3,4,5};
int[] a = new int[] {1,2,3,4,5}; (The size can be omitted)
3. Example
(1). Find the first 20 items of the sequence 1, 1, 2, 3, 5, 8... and sum them up.

//定义一个长度20的数组
int[] a = new int[20];
//先定义a[0],a[2]的值,然后通过相邻数相加得下一个数的规律,依次得出数组所有结果
 a[0] = 1;
 a[1] = 1;
int sum = 0;
for (int i = 2; i < 20; i++)
 {
    
    
   a[i] = a[i - 1] + a[i - 2];//得到数组中每一个下标对应的值
 }
for (int j = 0; j < 20; j++)
{
    
    
  Console.Write(a[j] + " ");//每循环一次,就输出一次
  sum += a[j];//同时把每一个数加起来
}
Console.WriteLine();//换行,看着比较美观,可不要
Console.WriteLine(sum);

(2). Define an integer array with a length of 10, and input 10 integers in a loop. Then enter an integer, search for the integer, find the output subscript, and give a prompt if it is not found

 Console.WriteLine("输入十个整数:");
 int[] a = new int[10];
 for (int i = 0; i < 10; i++)
  {
    
    
   Console.Write("第" + (i + 1) + "个整数:");
   int n = int.Parse(Console.ReadLine());
   a[i] = n;
  }
 Console.WriteLine("请输入一个整数:");
 int flag = 0;//判断输入的整数是否存在
 int num = int.Parse(Console.ReadLine());
 for (int j = 0; j < 10; j++)
   {
    
    
     if (num == a[j])
     {
    
    
      Console.WriteLine("找到其下标为:" + j);
      flag = 1;//如果存在,flag则为0
     }
   }
 if (flag == 0)
   {
    
     Console.WriteLine("没有找到");}

(3). Now there is an array as follows: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} Requirements Remove the items with a value of 0 in the above array, and store the values ​​that are not 0 into a new array, and the generated new array is: int newArr [] ={1,3,4,5,6,6,5, 4,7,6,7,5}

//思路:先找到该数组的长度,然后再把不为0的值存入新的数组
//自己做的方法太复杂了,可以跳过看下一个老师教的
 int[] oldArr = {
    
     1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };//长度16
 int n = 0;//判断0的数量,用来(oldArr.Length-n)得到非0数组的长度
 int length;
 for (int i = 0; i < oldArr.Length; i++)
   {
    
    
     if (oldArr[i] == 0){
    
    n++;}
   }
  int m = 0;//判断0的数量,
  length = oldArr.Length - n;//得到长度12
  int[] newArr = new int[length];//新定义一个数组
  //这个是新数组的下标
  for (int i = 0; i < length; i++)
    {
    
    
    //这个是旧数组的下标
    //假设i=6,则m=2,所以newArr[6]=oldArr[8]
    //此时newArr数组存了:1,3,4,5,6,6
     for (int k = i + m; k < oldArr.Length; k++)
      {
    
    
        if (oldArr[k] == 0)
         {
    
    
           m++;
           continue;
          }
          newArr[i] = oldArr[k];
          break;
          //若这里不中断for循环,
          //k就一直加到旧数组长度,newArr[i]就一直变换
      }
     }
 for (int j = 0; j < length; j++)
   {
    
    
     Console.Write(+newArr[j] + " ");
   }
int[] oldArr = {
    
     1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
int num = 0;
for (int i = 0; i < oldArr.Length; i++)
 {
    
    
  if (oldArr[i] == 0)
    {
    
    
     num++;
    }
 }
int[] newArr = new int[oldArr.Length - num];
int t = 0;
for (int i = 0; i < oldArr.Length; i++)
  {
    
    
    if (oldArr[i] == 0)
      {
    
    
             
      }else{
    
    
      //将不为0的数放在新数组中
      newArr[t]=oldArr[i];
      t++;
      }
  }
for (int i = 0; i < newArr.Length; i++)
{
    
    
 Console.Write(newArr[i]);
}

Guess you like

Origin blog.csdn.net/weixin_44706943/article/details/125726100