现在的位置: 首页 > 综合 > 正文

使用数组

2013年02月21日 ⁄ 综合 ⁄ 共 4179字 ⁄ 字号 评论关闭

使用数组>数组的长度获取,数组的遍历

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//获取数组长度
            Console.WriteLine("Hello,VS");
            
string[] csharpBooks = { "book1","book2","book3"};
            
int bookLength = csharpBooks.Length;//获取所有维度的元素的个数
            Console.WriteLine("一维数组的长度"+bookLength);

            string[,] csharpBooks2 = { { "book1""book2","book5" }, { "book3""book4" ,"book6"} };
            
int bookLength2=csharpBooks2.Length;
            Console.WriteLine(
"二维数组的所有元素个数" + bookLength2);
            Console.WriteLine(
"二维数组的指定维度的元素个数" + csharpBooks2.GetLength(0));
            

            //for循环遍历一维数组
            for (int index = 0; index <= csharpBooks.Length - 1; index++)
            {
                
string csharpBook = csharpBooks[index];
                Console.WriteLine(
"for循环遍历一维数组,循环结果  " + csharpBook);
            }

            //foreach循环遍历一维数组
            foreach (string csharpBook2 in csharpBooks)
            {
                Console.WriteLine(
"foreach循环遍历一维数组,循环结果  " + csharpBook2);
            }

            

        }
    }
}

 

使用数组>同步数组

同个时刻,多个线程,一个按列排列,一个多行排列。使用lock语句,让对象在同一时刻只执行一个操作,其他操作要等它完成才能执行。如果不用lock语句,那么多线程执行的不可控制。

代码
using System;
using System.Collections.Generic;
using System.Text;
//为了使用Thread类,必须在using区添加System.Threading
using System.Threading;

namespace LockArrayDemo
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//声明一个数组长度为100的一维数组
            string[] csharpBooks=new string[100];
            
//初始化数组元素
            for (int i = 0; i < 100; i++)
            {
                csharpBooks[i] 
= "book" + i;
            }
            
//定义第一个线程
            OperateArray oparray=new OperateArray(csharpBooks);
            Thread checkArray 
= new Thread(oparray.CheckArray);
            checkArray.Start();
            
//定义第二个线程
            Thread setArray = new Thread(oparray.SetArray);
            setArray.Start();
            Console.ReadLine();

        }
    }

    /// <summary>
    
/// 线程帮助类,定义了在线程中将要运行的方法
    
/// </summary>
    public class OperateArray
    {
        
private string[] bookArray;
        
public OperateArray(string[] array)
        {
            bookArray 
= array;
        }
        
public void CheckArray()
        {
            
//lock (bookArray)
            
//{
            for (int i = 0; i <= bookArray.GetUpperBound(0); i++)
            {
                
if (i>10&&/ 10 == 0)
                {
                    Console.WriteLine(bookArray[i]);
                }
                
else
                {
                    Console.Write(bookArray[i]
+"  ");
                }
            }
           
            
//}

        }
        
public void SetArray()
        {
           
lock (bookArray)
           {
                
foreach (string bookitem in bookArray)
                {
                    Console.WriteLine(bookitem);
                }
                
           }
        }
    }
}

 使用数组>数组排序

System.Array类提供了sort的静态方法,实现数组元素排序,但数组元素类型必须实现IComparer接口,否则抛出InvalidOperationException异常。意思就是手,数组元素要么是数字,要么是字母,才好排序,否则出错。

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//数组排序
            int[] array1 = { 123675 };
            System.Array.Sort(array1);
            Console.WriteLine(
"下面是使用内置的sort快速排序");
            
foreach (int elementArray in array1)
            {
                Console.WriteLine(elementArray);
            }
            

            Console.WriteLine("下面是使用自定义重写IComparer接口的反向排序");
            System.Array.Sort(array1, 
new MySortClass());
            
foreach (int elementArray in array1)
            {
                Console.WriteLine(elementArray);
            }
            Console.ReadLine();
        }
    }

    public class MySortClass : IComparer<int>
    {
        
public int Compare(int x, int y)
        {
            
if (x < y)
            {
                
return 1;
            }
            
else if (x > y)
            {
                
return -1;
            }
            
else
            { 
return 0; }

        }
    }
}

 

抱歉!评论已关闭.