You are on page 1of 91

7

7.1
7.2
7.3
7.4 foreach
7.5
7.6 Array

7.7

7.1

C#

7.1.1

[]

int[]
a;
string[] str;
C#
[ ]

7.1.1

int[5] a;
int b[];

//
//

7.1.2

new []


0.

a=new int[5];

int[] a=new int[5];

7.1.2

[]
1
0

7-1

using System;
public class CreateArray
{
public static void Main()
{
int[] a=new int[4];
double[] d=new double[3];
bool[] b=new bool[2];
object[] o=new object[3];
Console.Write("int");
for(int i=0;i<a.Length;i++)
Console.Write("\ta[{0}]={1}",i,a[i]);

7-1

Console.Write("\ndouble");
for (int i = 0; i < d.Length; i++)
Console.Write("\td[{0}]={1}", i, d[i]);
Console.Write("\nbool");
for (int i = 0; i < b.Length; i++)
Console.Write("\tb[{0}]={1}", i, b[i]);
Console.Write("\nobject");
for (int i = 0; i < o.Length; i++)
Console.Write("\to[{0}]={1}", i, o[i]);
Console.WriteLine();
}
}

7.1.2
C#
System.Array7-1
LengthArray

7-1

7.1.2

new

7-2
using System;
public class A
{
private int i;
public A(int a)
{
i = a;
}
public int I
{
get
{
return i;
}
}
}

7-2
public class ObjectArray
{
public static void Main()
{
A[] a = new A[3];
Console.WriteLine("\ta.Length={0}", a.Length);
for (int i = 0; i < a.Length; i++)
Console.Write("\ta[{0}]={1}", i, a[i]);
Console.WriteLine();
for (int i = 0; i < a.Length; i++)
a[i] = new A(2 * i);
for (int i = 0; i < a.Length; i++)
Console.Write("\ta[{0}].I={1}", i, a[i].I);
Console.WriteLine();
}
}

7.1.2
7-2
Length

Length


null

7.1.2

int[] b=new int[3]{1,2,3,};


int[] c=new int[4]{1,2,3,} ; //

int i=3;
int[] c=new int[i]{1,2,3,}; //
int[] d=new int[2+1]{1,2,3,};//

7.1.2

int[] b=new int[]{1,2,3}; //3



int[] b={1,2,3,};
//
int[] b;
b={1,2,3};
//

int[] b;
b=new int[]{1,2,3};//

7-3
using System;
public class A
{
private int i;
public A(int a)
{
i = a;
}
public int I
{
get
{
return i;
}
}
}

7-3
public class InitArray
{
public static void Main()
{
int[] b={1,2,3};
double[] d=new double[]{1.2,2.4};
A[] a1={new A(1),new A(2)};
A[] a2;
a2=new A[3]{new A(2),new A(4),new A(6),};
for(int i=0;i<b.Length;i++)
Console.Write("\tb[{0}]={1}\t",i,b[i]);
Console.WriteLine();

7-3
for (int i = 0; i < d.Length; i++)
Console.Write("\td[{0}]={1}", i, d[i]);
Console.WriteLine();
for (int i = 0; i < a1.Length; i++)
Console.Write("\ta1[" + i + "].I=" + a1[i].I);
Console.WriteLine();
for (int i = 0; i < a2.Length; i++)
Console.Write("\ta2[" + i + "].I=" + a2[i].I);
Console.WriteLine();
}

7.2

[ ,

, ] ;

nn+1

int[,] array ;
array= new int[3, 2];
int[, ,] array = new int[2, 3, 4];

7.2

int[,] array1 = new int[4,2]
{ { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
int[, ,] array2= new int[2,1,3]
{ { { 1, 2, 3 } }, { { 4, 5, 6 } } };
int[, ,] array2= new int[,,]
{ { { 1, 2, 3 } }, { { 4, 5, 6 } } };

7.2

int[,] array4= { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

7.2

[12]
0
1

using System;
public class DimArray
{
public static void Main()
{
int[,] array = { { 1, 2, 3 }, { 4, 5, 6 } };
for (int i = 0; i < array.GetLength(0); i++)
for (int j = 0; j < array.GetLength(1); j++)
Console.WriteLine("array[{0}][{1}]={2}", i, j, array[i, j]);
Console.WriteLine();
Console.WriteLine("array.GetLength(0)= {0}", array.GetLength(0));
Console.WriteLine("array.GetLength(1)= {0}", array.GetLength(1));
Console.WriteLine("array.Length= {0}", array.Length);
Console.WriteLine("array.Rank= {0}", array.Rank);
}
}
Length:
Rand:

GetLength:


10

using System;
class MaxMin
{
public static void Main()
{
int max, min;
int[] queue = new int[10] { 89, 78, 65, 52, 90, 92, 73, 85, 91, 95 };
max = min = queue[0];
for (int i = 1; i < 10; i++)
{
if (queue[i] > max) max = queue[i];
if (queue[i] < min) min = queue[i];
}
Console.WriteLine("{0}{1}", max, min);
}
}

7.3

[ ] [ ] [ ] ;
[ ]

int[][] jaggedArray = new int[3][];

3
int

7.3

null
new
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

7.3

jaggedArray[0] = new int[]


{ 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[]
{ 0, 2, 4, 6 };
jaggedArray[2] = new int[]
{ 11, 22 };

7.3

int[][] jaggedArray = new int[][]
{ new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22} };

int[][] jaggedArray3 = {
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22} };

7.3

jaggedArray3[0][1] = 77;
jaggedArray3[2][1] = 88;

7.3

int[][,] jaggedArray4 = new int[3][,]
{ new int[,] { {1,3}, {5,7} }, new int[,]
{ {0,2}, {4,6}, {8,10} }, new int[,]
{ {11,22}, {99,88}, {0,9} } };

System.Console.Write("{0}",
jaggedArray4[0][1, 1]);
//7
System.Console.Write("{0}",
jaggedArray4[1][2, 1]);
//10

7.3

Length

System.Console.WriteLine(
jaggedArray4.Length);

//3

7.3

int[][] array=new int[3][4];

using System;
class JaggedArray
{
static void Main()

int[][] arr = new int[2][];


arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
j
for (int i = 0; i < arr.Length; i++)

{
Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : ",");
}
Console.WriteLine();
}
}
}

7.4 foreach
foreach
foreach

foreach ( in )
;
foreach
foreach
foreach

7.4 foreach
foreach

1
foreach

foreach

7.4 foreach
foreach

int [] arr={1,2,3,4};
foreach(int i in arr)
{
i=0;
//
i++;
//
}

7.4 foreach


using System;
class A
{
int i;
public A(int j)
{
i = j; }
public int I
{
get { return i; }
set { i = value; }
}
}

7.4 foreach
class MyForeach
{
public static void Main()
{
A[] arr = { new A(1), new A(2) };
Console.WriteLine(arr[1].I);
foreach (A a in arr)
a.I = 0;
//
Console.WriteLine(arr[1].I);
}
}

7-6 foreach
using System;
class MyForeach
{
public static void Main()
{
int result = 0;
int[] arr = { 1, 2, 3, 4 };
foreach (int i in arr)
result += i;
Console.WriteLine("Result={0}", result);
}
}

7.4 foreach

foreach

int[,] numbers2D = new int[3, 2]


{ { 9, 99 }, { 3, 33 }, { 5, 55 } };
foreach (int i in numbers2D)
System.Console.Write("{0} ", i);

7.4 foreach
foreach

int[][] array={new int[]{1,2},new int[]{3},new int[]{4,5,6}};



foreach(int[] i in array)
{
foreach(int j in i)
System.Console.WriteLine(j);
}

7.4 foreach

foreach(int i in array)
System.Console.WriteLine(i);

arrayint[]
int


>=60
using >=60foreach
System;
class ForExam
{
public static void Main()
{
int count;
int[,] arrayNum = new int[3, 4] { { 98, 76, 87, 65 }, { 55, 68, 57, 84 }, { 91, 100, 58, 76 } };
count = 0;
foreach (int x in arrayNum)
if (x >= 60)
{
count++;
Console.Write("{0,4:d}", x);
}
}
}

7.5

7.5.1
7.5.2
7.5.3 Main
7.5.4

7.5.1

7-7
using System;
public class PassByArray
{
public static void Main()
{
int[] array1={1,2,3};
int[] array2=array1;
int[] array3;

7-7
Console.WriteLine("");
Console.Write("");
foreach(int i in array1)
Console.Write(" {0}",i);
Console.WriteLine();
FirstDouble(array1);
Console.Write("");
foreach(int i in array1)
Console.Write(" {0}",i);
Console.WriteLine("\n");

7-7
Console.WriteLine("");
Console.Write("");
foreach(int i in array2)
Console.Write(" {0}",i);
Console.WriteLine();
SecondDouble(ref array2);
Console.Write("");
foreach(int i in array2)
Console.Write(" {0}",i);
Console.WriteLine("\n");

7-7
Console.WriteLine("");
Console.WriteLine("");
FillArray(out array3);
Console.Write("");
foreach(int i in array3)
Console.Write(" {0}",i);
Console.WriteLine("\n");
Console.WriteLine("");

PrintArray(new int[,] {
{ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
}

7-7
public static void FirstDouble(int[] array)
{
for(int i=0;i<array.Length;i++)
array[i]*=2;
array=new int[]{11,22,33};
}
public static void SecondDouble(ref int[] array)
{
for(int i=0;i<array.Length;i++)
array[i]*=2;
array=new int[]{11,22,33};
}

7-7
public static void FillArray(out int[] arr)
{
arr = new int[5] { 1, 2, 3, 4, 5 };
}
public static void PrintArray(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
Console.WriteLine("\tElement({0},{1})={2}", i, j, arr[i, j]);

}
}
}

7.5.2
params

class A
{

public static void Method(params int[] array){}


}

7.5.2

A.Method(1,2)

int[] array=new int[]{1,2};


A.Method(array);

A.Method(1,2,3)

7.5.2

7-8
using System;
public class UseParams
{
public static void ParamsMethod1(params int[] array)
{
for (int i = 0 ; i < array.Length; i++)
{
Console.Write("\t{0}",array[i]);
}
Console.WriteLine("\n");
}

7-8
public static void ParamsMethod2(params object[] array)
{
for (int i = 0 ; i < array.Length; i++)
{
Console.Write("\t{0}",array[i]);
}
Console.WriteLine("\n");
}

7-8
static void Main()
{
ParamsMethod1(1, 2, 3)
//object

ParamsMethod2(11, 'a', "bbb",44);


int[] array = new int[3] { 111, 222, 333 };
ParamsMethod1(array);

//

7.5.2
params

ref
out

params

7.5.2
1.
int Method(params int[,] array){}
//
int Method(params int[][] array){} //,

2.
int Method(params int[] array,int a){}
int Method(int a ,params int[] array){}

//
//

int Method(ref params int[] array){}


int Method(out params int[] array){}

//
//

int Method(params int[] array){}


int Method(int[] array){}

//

3.

5.

7.5.3 Main
MainC#

Main4
1.static
2.static
3.static
4.static

void Main()
int Main()
void Main(string[] args)
int Main(string[] args)

2.4.00
3.4.args

7.5.3 Main
Main
Mainargs
1args [0]
2args [1]
......

7-9
using System;
public class CommandLine
{
public static void Main(string[] args)
{
Console.WriteLine("parameter count = {0}", args.Length);
foreach (string s in args)
{
Console.WriteLine(s);
}
}
}

7-9

7-9 .Net

7-9 .Net

7.5.4

using System;
public class ReturnArray
{
public static void Main()
{
int[] a1 = { 10, 4, 2 };
int[] a2;
for (int i = 0; i < a1.Length; i++)
Console.Write(" a1[{0}]={1}", i, a1[i]);
Console.WriteLine();
a2 = CopyArray(a1);
for (int i = 0; i < a2.Length; i++)
Console.Write(" a2[{0}]={1}", i, a2[i]);
Console.WriteLine();
}
public static int[] CopyArray(int[] la)
{
int[] a = new int[la.Length];
for (int i = 0; i < la.Length; i++)
a[i] = la[i];
return a;
}
}

7.6 Array
Array


System.Object
System.Array

7.6.1
7.6.2
7.6.3

7.6.1
Array.Copy
CopyTo

7.6.1
Array
Array 32
public static void Copy( Array sourceArray, Array
destinationArray, int length )

sourceArray
System.Array
Array

destinationArray
System.Array
Array

length
System.Int32
32

Array
Array 32
public static void Copy( Array sourceArray, int sourceIndex, Array
destinationArray, int destinationIndex, int length )

sourceArray
System.Array
Array

sourceIndex
System.Int32
32 sourceArray

destinationArray
System.Array
Array

destinationIndex
System.Int32
32 destinationArray

length
System.Int32
32

7.6.1
Array Array
Array 32

public void CopyTo( Array array, int index )



array
System.Array
Array Array

index
System.Int32
32 array

7-11
using System;
public class ArrayCopy
{
public static void Main()
{
int[] arr1=new int[]{1,2,3,4,5};
object[] arr2=new object[]{10,20,30,40,50,60};
Console.Write( "Int32 array:" );
PrintArray( arr1);
Console.Write( "Object array:" );
PrintArray( arr2 );
Console.WriteLine();

Array.Copy( arr2, arr1, 3 );


Console.WriteLine( "Int32 array3Object array
:" );
Console.Write( "Int32 array:" );
PrintArray( arr1);
Console.Write( "Object array:" );
PrintArray( arr2 );
Console.WriteLine();
Array.Copy( arr1, arr1.Length-2, arr2, arr2.Length-2, 2 );
Console.WriteLine( "Object array2Int32
array:" );
Console.Write( "Int32 array:" );
PrintArray( arr1);
Console.Write( "Object array:" );
PrintArray( arr2 );
Console.WriteLine();

arr1.CopyTo(arr2, arr2.Length - arr1.Length);


Console.WriteLine("Object arrayInt32 array
:");
Console.Write("Int32 array:");
PrintArray(arr1);
Console.Write("Object array:");
PrintArray(arr2);
}
public static void PrintArray(Array myArr)
{
foreach (object i in myArr)
{
Console.Write("\t{0}", i);
}
Console.WriteLine();
}
}

7.6.2
Array.sort


System.IComparable
System.IComparer

7.6.2
IComparableIComparer

IComparable

IComparer

IComparable.NET

7.6.2

System.IComparable
IComparableCompareTo
Object

using System;
class A : IComparable
{
int i, j;
public A(int a1, int a2)
{ i = a1; j = a2; }
public int CompareTo(Object obj)
{
int k = ((A)obj).i;
if (k > i)
return -1;
else if (k < i)
return 1;
else
return 0;
}
public override String ToString()
{ return "(" + i + "," + j + ")"; }
}

public class ArraySort


{
public static void Main()
{
int[] a1={5,2,6,4,3,8,1,7};
Console.WriteLine(":");
PrintArray(a1);
Array.Sort(a1,0,3);
Console.WriteLine("0~2:");
PrintArray(a1);
Array.Sort(a1);
Console.WriteLine(":");
PrintArray(a1);

A[] a2 = { new A(5, 2), new A(2, 3), new A(3, 1) };


Console.WriteLine(":");
PrintArray(a2);
Array.Sort(a2);
Console.WriteLine(":");
PrintArray(a2);
}
public static void PrintArray(Array myArr)
{
foreach (object i in myArr)
{
Console.Write("{0}\t", i);
}
Console.WriteLine();
}
}

7.6.2

IComparable
CompareTo

using System;
class A : IComparable
{
int i, j;
public A(int a1, int a2)
{ i = a1; j = a2; }
public int CompareTo(Object obj)
{
int k = ((A)obj).j;
if (k > j)
return -1;
else if (k < j)
return 1;
else
return 0;
}
public override String ToString()
{ return "(" + i + "," + j + ")"; }
}

public class ObjectCompare


{
public static void Main()
{
A a1 = new A(2, 4);
A a2 = new A(5, 3);
if (a1.CompareTo(a2) > 0)
Console.WriteLine("{0}>{1}", a1, a2);
else if (a1.CompareTo(a2) < 0)
Console.WriteLine("{0}<{1}", a1, a2);
else
Console.WriteLine("{0}=={1}", a1, a2);
}
}

7.6.3
Array.BinarySearch

IComparable
IComparer

IComparable
IComparer

using System;
class A : IComparable
{
int i, j;
public A(int a1, int a2)
{ i = a1; j = a2; }
public int CompareTo(Object obj)
{
int k = ((A)obj).i;
if (k > i)
return -1;
else if (k < i)
return 1;
else
return 0;
}
public override String ToString()
{ return "(" + i + "," + j + ")"; }
}

public class ArraySearch


{
public static void Main()
{
int loc;
int[] a1 = { 5, 2, 6, 4, 3, 8, 1, 7 };
Array.Sort(a1);
PrintArray(a1);
int i = 6, j = 10;
loc = Array.BinarySearch(a1, i);
if (loc >= 0)
Console.WriteLine("a1[" + loc + "]=" + i);
else
Console.WriteLine("" + i);
loc = Array.BinarySearch(a1, j);
if (loc >= 0)
Console.WriteLine("a1[" + loc + "]=" + j);
else
Console.WriteLine("" + j);

A[] a2 = { new A(5, 2), new A(2, 3), new A(3, 1) };


Array.Sort(a2);
PrintArray(a2);
A aobj1 = new A(3, 1);
A aobj2 = new A(4, 2);
loc = Array.BinarySearch(a2, aobj1);
if (loc >= 0)
Console.WriteLine("a2[" + loc + "]=" + aobj1);
else
Console.WriteLine("" + aobj1);
loc = Array.BinarySearch(a2, aobj2);
if (loc >= 0)
Console.WriteLine("a2[" + loc + "]=" + aobj2);
else
Console.WriteLine("" + aobj2);

}
public static void PrintArray(Array myArr)
{
foreach (object i in myArr)
{
Console.Write("{0}\t", i);
}
Console.WriteLine();
}
}

7.7
1.

2.

new

3.C#
ArrayLength

4.Length
Length
Length

5.int21041220
24
6.Circle
radius5Circle
radius2108412
Array.Sort()