使用数组2
使用数组>查找数组>BinarySearch二进制搜索
BinarySearch找到匹配的元素值,返回指定元素的索引。找不到,返回一个负值。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//查找数组>BinarySearch二进制搜索
string[] csharpBooks = new string[3];
csharpBooks[0] = "book1";
csharpBooks[1] = "book3";
csharpBooks[2] = "book2";
Array.Sort(csharpBooks);//BinarySearch要求搜寻的数组必须先排序,并且按升序排序。数组中的每个元素要实现IComparable接口
foreach (string csharpBook in csharpBooks)
{
Console.WriteLine(csharpBook);
}
Console.WriteLine("使用二分算法搜寻数组元素值book2:");
object searchBook = "book2";
int searchResult = Array.BinarySearch(csharpBooks, searchBook);
if (searchResult < 0)
{
Console.WriteLine("搜寻数组元素值{0},没有找到!", searchBook);
}
else
{
Console.WriteLine("搜寻数组元素值{0},已找到,在索引位置{1}", searchBook, searchResult);
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//查找数组>BinarySearch二进制搜索
string[] csharpBooks = new string[3];
csharpBooks[0] = "book1";
csharpBooks[1] = "book3";
csharpBooks[2] = "book2";
Array.Sort(csharpBooks);//BinarySearch要求搜寻的数组必须先排序,并且按升序排序。数组中的每个元素要实现IComparable接口
foreach (string csharpBook in csharpBooks)
{
Console.WriteLine(csharpBook);
}
Console.WriteLine("使用二分算法搜寻数组元素值book2:");
object searchBook = "book2";
int searchResult = Array.BinarySearch(csharpBooks, searchBook);
if (searchResult < 0)
{
Console.WriteLine("搜寻数组元素值{0},没有找到!", searchBook);
}
else
{
Console.WriteLine("搜寻数组元素值{0},已找到,在索引位置{1}", searchBook, searchResult);
}
Console.ReadLine();
}
}
}
使用数组>查找数组>Find搜索和IndexOf搜索
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//查找数组>Find搜索
string[] csharpBooks = { "book1", "book2", "book3", "book4", "book5" };
//传递一个数组和一个委托到Find方法,Find方法将以每个数组元素为参数调用GetSharpBook方法
string firstCsharpBooks = Array.Find<string>(csharpBooks, GetCSharpBook);
//如果找到就定位到第一个元素。
Console.WriteLine("找到了数组元素值为{0}的数组元素", firstCsharpBooks);
//Console.ReadLine();
//-------------------------------------------------
//查找数组>indexOf搜索
Array IndexOfcsharpBooks = Array.CreateInstance(typeof(string), 6);
IndexOfcsharpBooks.SetValue("book1", 0);
IndexOfcsharpBooks.SetValue("book2", 1);
IndexOfcsharpBooks.SetValue("book3", 2);
IndexOfcsharpBooks.SetValue("book4", 3);
IndexOfcsharpBooks.SetValue("book5", 4);
IndexOfcsharpBooks.SetValue("book6", 5);
string csharpBook = "book5";
int searchResultIndex = Array.IndexOf(IndexOfcsharpBooks, csharpBook);
Console.WriteLine("{0}在IndexOfcsharpBooks数组中首次出现的位置是{1}", csharpBook, searchResultIndex);
}
//定义一个执行查找元素的方法
private static bool GetCSharpBook(string csharpbook)
{
if (csharpbook == "book2")
{
return true;
}
else
{
return false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//查找数组>Find搜索
string[] csharpBooks = { "book1", "book2", "book3", "book4", "book5" };
//传递一个数组和一个委托到Find方法,Find方法将以每个数组元素为参数调用GetSharpBook方法
string firstCsharpBooks = Array.Find<string>(csharpBooks, GetCSharpBook);
//如果找到就定位到第一个元素。
Console.WriteLine("找到了数组元素值为{0}的数组元素", firstCsharpBooks);
//Console.ReadLine();
//-------------------------------------------------
//查找数组>indexOf搜索
Array IndexOfcsharpBooks = Array.CreateInstance(typeof(string), 6);
IndexOfcsharpBooks.SetValue("book1", 0);
IndexOfcsharpBooks.SetValue("book2", 1);
IndexOfcsharpBooks.SetValue("book3", 2);
IndexOfcsharpBooks.SetValue("book4", 3);
IndexOfcsharpBooks.SetValue("book5", 4);
IndexOfcsharpBooks.SetValue("book6", 5);
string csharpBook = "book5";
int searchResultIndex = Array.IndexOf(IndexOfcsharpBooks, csharpBook);
Console.WriteLine("{0}在IndexOfcsharpBooks数组中首次出现的位置是{1}", csharpBook, searchResultIndex);
}
//定义一个执行查找元素的方法
private static bool GetCSharpBook(string csharpbook)
{
if (csharpbook == "book2")
{
return true;
}
else
{
return false;
}
}
}
}
使用数组>反转数组
何谓反转数组,就是123木头人,变成人头木321。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] csharpBooks = { "book1", "book2", "book5", "book7", "book8", "book9" };
Console.WriteLine("在没有调用Reverse的数组顺序为:");
printArray(csharpBooks);
//调用Array.Reverse反转数组的顺序
Array.Reverse(csharpBooks);
Console.WriteLine("调用Reverse方法后的数组顺序为:");
printArray(csharpBooks);
Console.ReadLine();
}
static void printArray(string[] array)
{
foreach (string element in array)
{
Console.WriteLine(element);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] csharpBooks = { "book1", "book2", "book5", "book7", "book8", "book9" };
Console.WriteLine("在没有调用Reverse的数组顺序为:");
printArray(csharpBooks);
//调用Array.Reverse反转数组的顺序
Array.Reverse(csharpBooks);
Console.WriteLine("调用Reverse方法后的数组顺序为:");
printArray(csharpBooks);
Console.ReadLine();
}
static void printArray(string[] array)
{
foreach (string element in array)
{
Console.WriteLine(element);
}
}
}
}
使用数组>复制数组
有两种方法,System.Array.Copy是静态方法,System.Array.Copyto是实例方法。都可以将一个数组的一部分元素复制到另外一个数组中。
代码
using System;
using System.Collections.Generic;
using System.Text;
namespace CopyArray
{
class Program
{
static void Main(string[] args)
{
string[] csharpBooks ={ "book1", "book2", "book5", "book7", "book8", "book9" };
string[] csharpBooks2=new string[csharpBooks.Length];
string[] csharpBooks3 = new string[csharpBooks.Length];
Console.WriteLine("源数组的内容为:");
PrintArray(csharpBooks);
//使用Copy复制数组
Array.Copy(csharpBooks, csharpBooks2, 4);
Console.WriteLine("使用copy方法复制到的数组内容为:");
PrintArray(csharpBooks2);
//使用Copyto复制数组
csharpBooks.CopyTo(csharpBooks3,0);
Console.WriteLine("使用copyTo方法复制到的数组内容为:");
PrintArray(csharpBooks3);
Console.ReadLine();
}
static void PrintArray(string[] array)
{
foreach (string element in array)
{
Console.WriteLine(element);
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace CopyArray
{
class Program
{
static void Main(string[] args)
{
string[] csharpBooks ={ "book1", "book2", "book5", "book7", "book8", "book9" };
string[] csharpBooks2=new string[csharpBooks.Length];
string[] csharpBooks3 = new string[csharpBooks.Length];
Console.WriteLine("源数组的内容为:");
PrintArray(csharpBooks);
//使用Copy复制数组
Array.Copy(csharpBooks, csharpBooks2, 4);
Console.WriteLine("使用copy方法复制到的数组内容为:");
PrintArray(csharpBooks2);
//使用Copyto复制数组
csharpBooks.CopyTo(csharpBooks3,0);
Console.WriteLine("使用copyTo方法复制到的数组内容为:");
PrintArray(csharpBooks3);
Console.ReadLine();
}
static void PrintArray(string[] array)
{
foreach (string element in array)
{
Console.WriteLine(element);
}
}
}
}
使用数组>复制数组
这个我也有点看不明白,代码还是先放上吧。运行时才检测数组元素的类型以及数组长度。
代码
using System;
using System.Collections.Generic;
using System.Text;
namespace DynamicCreateInstance
{
class Program
{
static void Main(string[] args)
{
//动态创建一个5个元素的一维string数组
Array csharpBooks = Array.CreateInstance(typeof(string), 5);
for (int i = 0; i <= csharpBooks.GetUpperBound(0); i++)
{
csharpBooks.SetValue("book" + (i+1), i);
}
foreach (string csharpbook in csharpBooks)
{
Console.WriteLine(csharpbook);
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace DynamicCreateInstance
{
class Program
{
static void Main(string[] args)
{
//动态创建一个5个元素的一维string数组
Array csharpBooks = Array.CreateInstance(typeof(string), 5);
for (int i = 0; i <= csharpBooks.GetUpperBound(0); i++)
{
csharpBooks.SetValue("book" + (i+1), i);
}
foreach (string csharpbook in csharpBooks)
{
Console.WriteLine(csharpbook);
}
Console.ReadLine();
}
}
}
合乎自然而生生不息。。。