值类型:int,char,double,…
引用类型:数组
int d1=10;
int d2=d1;
d2=100; d1=?
int[] x1=new int[10];
int[] x2=x1;
x2[3]=100; x1[0]=?
string[] names={“zhou”,”zhang”,”liu”,”wangping”};
names[2]=”lisi”;
int find(int[] arr, int x)
{
int index=-1;
for(int i=0;i<arr.Length;i++)
if(arr[i]==x){
index=i;break;
}
return index;
}
static void reverse(int[] arr)
{
for (int i = 0; i < arr.Length / 2; i++)
{
int t=arr[i];
arr[i]=arr[arr.Length-i-1];
arr[arr.Length - i - 1] = t;
}
}
static int quickFind(int[] arr, int x)//形式参数
{
int index = -1;
int low = 0, high = arr.Length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == x)
{
index = mid; break;
}
else if (arr[mid] > x)
high = mid - 1;
else
low = mid + 1;
}
return index;
}
选择排序算法:每次从未排序区找出最小的元素与未排序区域顶部元素交换。
2, 3, 1, 0, 7, 4
0, 3, 1, 2, 7, 4
0, 1, 3, 2, 7, 4
0, 1, 2, 3, 7, 4
0, 1, 2, 3, 7, 4
0, 1, 2, 3, 4, 7
void selectSort(int[] arr){
for(int i=0;i<arr.Length-1;i++){
int min=i;
for(int j=i+1;j<arr.Length;j++)
if(arr[j]<arr[min]) min=j; //找出最小位置
if(min!=i){
int t=arr[i];arr[i]=arr[min];arr[min]=t;
}
}
}
整个示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ex2013_07_22
{
class Ex1_1
{
static void selectSort(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
int min = i;
for (int j = i + 1; j < arr.Length; j++)
if (arr[j] < arr[min]) min = j;
//找出最小位置
if (min != i)
{
int t = arr[i];
arr[i] = arr[min];
arr[min] = t;
}
}
}
static int quickFind(int[] arr, int x)//形式参数
{
int index = -1;
int low = 0, high = arr.Length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == x)
{
index = mid; break;
}
else if (arr[mid] > x)
high = mid - 1;
else
low = mid + 1;
}
return index;
}
static int find(int[] arr, int x)//形式参数
{
int index = -1;
for (int i = 0; i < arr.Length; i++)
if (arr[i] == x)
{
index = i; break;
}
return index;
}
static void reverse(int[] arr)
{
for (int i = 0; i < arr.Length / 2; i++)
{
int t=arr[i];
arr[i]=arr[arr.Length-i-1];
arr[arr.Length - i - 1] = t;
}
}
static void disp(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i]+" ");
Console.WriteLine();
}
static void Main(string[] args)
{
int[] arr1 = { 5,2,0,4,-2,8};
disp(arr1);
selectSort(arr1);
disp(arr1);
Console.ReadLine();
}
}
}
1)声明二维数组变量
int[,] x;
x=new int[3,5];
eg:初始化一个二维数组
static int[,] initArray(int rows,int cols){
int[,] arr=new int[rows,cols];
}
eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ex2013_07_22
{
class Ex1_1
{
static int[,] initArray(int rows, int cols)
{
int[,] arr = new int[rows, cols];
Random rnd=new Random();
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
arr[i, j] =60+ rnd.Next(41);
return arr;
}
static void disp(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
Console.Write("{0,-4}",arr[i,j]);
Console.WriteLine();
}
}
static int[,] reverse(int[,] arr)
{
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);
int[,] rarr=new int[cols,rows];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
rarr[j,i]=arr[i,j];
}
return rarr;
}
static void Main(string[] args)
{
int[,] x = initArray(5,10);
disp(x);
int[,] rx=reverse(x);
disp(rx);
Console.ReadLine();
}
}
}
int[][] x = new int[5][];
Console.WriteLine(x.Length);
x[0]=new int[10];
x[0]. Length
本章练习题下载地址:点此下载