//数组是一系列相同类型的元素的集合(综合体)
int[] arr = new int[3];
//定义了一个int类型数组,里面可以存放三个int类型的数
//通过下标来取数组中的某个元素,下标从0开始
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
int a = arr[1];
Console.WriteLine("第二个元素{0}",a);
所有的数据类型都可以有对应的数组
数组里面元素的个数在定义的时候就确定下来了
数组里面元素的个数是固定的
double[] arrd = new double[3];
//arrd =new double[4];//new 分配新的内存,是一个新的数组
当数组里面的每一个子元素的类型为值类型时,定义这个数组后子元素的默认值为0;
当数组里的子元素的类型为引用类型时,定义这个数组后子元素的类型默认值为null
string sa = null;
string sa = "";
null代码为变量还没有分配内存,“”是空字符串,分配了内存
int[] arr = { 1,2,3,4};
//克隆
//克隆clone()克隆的是这个数组内存上的内容
object obj= arr.Clone();
int[] arr2=(int[])obj;
arr2[0] = 8;
Console.WriteLine(arr[0]);
//赋值,赋的是这个变量的引用地址,两个变量指向了同一地址
int[] arr3 = arr;
arr[0] = 10;
Console.WriteLine(arr[0]);
二维数组
int r = arr.Rank;//数组的维数
Console.WriteLine("长度{0}",arr.Length);//总长度
int e=arr.GetLength(0);//获取第一维的长度
Console.WriteLine("第一维的长度{0}",e);//2
arr.GetUpperBound(0);//获取第一维的最大下标
arr.GetLowerBound(0);//获取第一维的最小下标
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
数组是一系列相同类型的元素的集合(综合体)
//int[] arr = new int[3];
定义了一个int类型数组,里面可以存放三个int类型的数
通过下标来取数组中的某个元素,下标从0开始
//arr[0] = 10;
//arr[1] = 20;
//arr[2] = 30;
//int a = arr[1];
//Console.WriteLine("第二个元素{0}",a);
//foreach (int item in arr)
//{
// Console.WriteLine(item);
//}
属性
//Console.WriteLine("长度{0}",arr.Length);
//for (int i = 0; i < arr.Length; i++)
//{
// Console.WriteLine(arr[i]);
//}
//double[] arrd=new double[10];
//string[] arrs = new string[5];
//所有的数据类型都可以有对应的数组
//数组里面元素的个数在定义的时候就确定下来了
//数组里面元素的个数是固定的
//double[] arrd = new double[3];
//arrd =new double[4];//new 分配新的内存,是一个新的数组
//arrd[1] = 20;
//Console.WriteLine(arrd[0]);
//string[] arrs = new string[3];
//arrs[1] = "abc";
//string s = arrs[0];
//Console.WriteLine(s);
//当数组里面的每一个子元素的类型为值类型时,定义这个数组后子元素的默认值为0;
//当数组里的子元素的类型为引用类型时,定义这个数组后子元素的类型默认值为null
//string sa = null;
//string sa = "";
//null代码为变量还没有分配内存,“”是空字符串,分配了内存
//Student[] stu = new Student[3];
//Student s;
//s.Name = "小黄";
//s.Sex = "男";
//s.Age = 20;
// stu[0]=s;
// stu[1].Name = "sadas";
// stu[1].Sex = "男";
//stu[1].Age=21;
//stu[2].Name="qwe";
//stu[2].Sex="nv";
//stu[2].Age=25;
//foreach (var e in stu)
//{
// Console.WriteLine("{0}\t{1}\t{2}",e.Name,e.Sex,e.Age);
//}
//数组的初始化
//string s;
//s = "abc";
//string str = "abc";
//int[] arr=new int[3];
//double[] arrdb = new double[3] { 1,2,3};
//Console.WriteLine(arrdb[0]);
//double[] arrbd2 = { 1,2,3,4,5,6,7,8,9,0};
//Console.WriteLine(arrbd2.Length);
//int[] arr = { 1, 2, 3, 4 };
//克隆
//克隆clone()克隆的是这个数组内存上的内容
//object obj= arr.Clone();
//int[] arr2=(int[])obj;
//arr2[0] = 8;
//Console.WriteLine(arr[0]);
//赋值,赋的是这个变量的引用地址,两个变量指向了同一地址
//int[] arr3 = arr;
//arr[0] = 10;
//Console.WriteLine(arr[0]);
//包涵
//bool b = arr.Contains(10);
//Console.WriteLine(b);
//int count = arr.Count();
//Console.WriteLine(count);
//int max = arr.Max();
//int min = arr.Min();
//Array 是所有数组的基类
//int[] arrnew=new int[5];
//arr.CopyTo(arrnew,1);
将arr中的元素赋值到arrNew中,从arrNew的第二个元素开始填充
//foreach (var item in arrnew)
//{
// Console.WriteLine(item);
//}
//Array的方法****************************
//Array.Clear(arr,0,2);
//foreach (var item in arr)
//{
// Console.WriteLine(item);
//}
//int[] arr2=new int[5];
//Array.Copy(arr,arr2,2);//复制
//foreach (var item in arr2)
//{
// Console.WriteLine(item);
//}
//arr = new int[] { 6,8,5,4};
//Array.Sort(arr);//排序 默认从小到大
//Array.Reverse(arr);//反转
//输入10个数,求这10个数的和
//int[] arr=new int[5];
//for (int i = 0; i < 5; i++)
//{
// Console.WriteLine("请输入一个数");
// arr[i] = int.Parse(Console.ReadLine());
//}
//foreach (var item in arr)
//{
// Console.WriteLine(item);
//}
//冒泡排序**********************************************
//沉石排序
//int[] arr={2,5,3,8,4,6,1};
//for (int j = 0; j < arr.Length-1; j++)//控制比较的次数
//{
// //j 0到5
// for (int i = 0; i < arr.Length-1-j; i++)//控制比较的个数
// {
// //arr[i]与arr[i+1]比较
// //如果arr[i]>arr[i+1],两者交换
// if (arr[i]>arr[i+1])
// {
// int temp = arr[i];
// arr[i] = arr[i + 1];
// arr[i + 1] = temp;
// }
// }
//}
//foreach (var item in arr)
//{
// Console.WriteLine(item);
//}
//**************二维及多维数组
//二维数组,可以理解为一个面,面上的一个点需要两个下标来确定
//int[,] arr = new int[2, 3];
//arr[0, 0] = 10;
//arr[0, 1] = 20;
//arr[0,2] = 30;
//arr[1, 0] = 40;
//arr[1, 1] = 50;
//arr[1, 2] = 60;
//int r = arr.Rank;//数组的维数
//Console.WriteLine("长度{0}",arr.Length);//总长度
//int e=arr.GetLength(0);//获取第一维的长度
//Console.WriteLine("第一维的长度{0}",e);//2
arr.GetUpperBound(0);//获取第一维的最大下标
arr.GetLowerBound(0);//获取第一维的最小下标
foreach (int item in arr)
{
Console.WriteLine(item);
}
//for (int j = 0; j < arr.GetLength(0); j++)
//{
// for (int i = 0; i < arr.GetLength(1); i++)
// {
// Console.Write("{0}\t", arr[j, i]);
// }
// Console.WriteLine();//换行
//}
//
int n=4;
int[,] arr = new int[n, n];
for (int i = 0; i < n; i++)
{
//第一行
arr[0, i] = 1;
arr[n - 1, i] = 1;
arr[i, 0] = 1;
arr[i, n - 1] = 1;
}
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write("{0}",arr[i,j]==0?"":"1");
}
Console.WriteLine();
}
}
}
/// <summary>
/// 学生的结构
/// </summary>
struct Student
{
public string Name;
public string Sex;
public int Age;
}
}
1. 1 2 3 5 3 5 7 9
4 5 6 2 4 0 3 0
5 6 2 3 + 2 3 0 8 =?,用C#程序求出以上的结果.
8 5 3 1 9 7 5 1
}
int[,] arr1 = { { 1, 2,3,5 }, { 4,5,6,2 }, { 5, 6,2,3 } ,{8,5,3,1}};
int[,] arr2 = { {3,5,7,9},{4,0,3,0},{2,3,0,8},{9,7,5,1} };
int[,] arr3 = new int[4, 4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
arr3[i, j] = arr1[i, j] + arr2[i, j];
Console.Write("{0}\t", arr3[i, j]);
}
}
Console.WriteLine();
2. 输入5个数,将其从大到小排列输出。(使用冒泡)。
int[] arr = new int[5];
for (int i = 0; i < 5; i++)
{
Console.WriteLine("输入第{0}个值", i + 1);
arr[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] < arr[j + 1])
{
arr[j] = arr[j] + arr[j + 1];
arr[j + 1] = arr[j] - arr[j + 1];
arr[j] = arr[j] - arr[j + 1];
}
foreach (int item in arr)
{
Console.WriteLine(item);
}
}
}
3. 打印出杨辉三角形(要求打印出10行如下图)
1
1 1
1 2 1
1 3 3 1
7. 2 4 9 1 2
1 3 5 * 4 5 =?,用C#程序求出以上的结果.
1 0
27 24
18 17
3.输入一个”3”打印如下:
1 1 1
1 2 1
1 1 1
输入一个”5”打印如下
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
5.输入一五个数,用数组接收,将最大的数与第一个数交换,最小的数与最后一个数交换,其它数不变.
int[] a=new int [5];
for (int i = 0; i <5; i++)
{
a[i]=int.Parse (Console.ReadLine());
}
int b = a.Max();
int c = Array.IndexOf(a,b);
int d = a.Min();
int e = Array.IndexOf(a, d);
if (a[c]>a[0])
{
a[c] = a[c] + a[0];
a[0] = a[c] - a[0];
a[c] = a[c] - a[0];
}
if (a[e]<a[4])
{
a[e] = a[e] + a[4];
a[4] = a[e] - a[4];
a[e] = a[e] - a[4];
}
foreach (int item in a)
{
Console.WriteLine(item );
}
8 有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
int[] a = { 12, 15, 17, 19, 21 };
foreach (int item in a)
{
Console.Write(item + " ");
}
Console.WriteLine();
int b = int.Parse(Console.ReadLine());
int[] c = { 12, 15, 17, 19, 21 ,b};
Array.Sort(c);
foreach (int item in c)
{
Console.Write(item+" ");
}
Console.WriteLine();
9 将一个数组逆序输出。
int[] a = { 12, 15, 17, 19, 21 };
foreach (int item in a)
{
Console.Write(item + " ");
}
Console.WriteLine();
Array.Reverse(a);
foreach (int item in a)
{
Console.Write(item+" " );
}
Console.WriteLine();
10 精确求出100!