Csharp/CSharp Tutorial/Data Structure/Array

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A one-dimensional array.

An array is a collection of variables of the same type that are referred to by a common name.

To declare a one-dimensional array, you will use this general form:


<source lang="csharp">type[ ] array-name = new type[size];</source>

Array Declaration with initialization

The general form for initializing a one-dimensional array is shown here:


<source lang="csharp">type[ ] array-name = { val1, val2, val3, ..., valN };</source>

Array Inherited Members: get array type

<source lang="csharp">using System; class MainClass {

  static void Main()
  {
     int[] arr = new int[] { 15, 20, 5, 25, 10 };
     Console.WriteLine("GetType()    = {0}", arr.GetType());
  }

}</source>

GetType()    = System.Int32[]

Array Inherited Members: GetLength(0)

<source lang="csharp">using System; class MainClass {

  static void Main()
  {
     int[] arr = new int[] { 15, 20, 5, 25, 10 };
     Console.WriteLine("GetLength(0) = {0}", arr.GetLength(0));
  }

}</source>

GetLength(0) = 5

Array Inherited Members: Rank and Length

<source lang="csharp">using System; class MainClass {

  static void Main()
  {
     int[] arr = new int[] { 15, 20, 5, 25, 10 };
     Console.WriteLine("Rank = {0}, Length = {1}", arr.Rank, arr.Length);
  }

}</source>

Rank = 1, Length = 5

Declare the array and instantiate the array

<source lang="csharp">using System; class MainClass {

  static void Main()
  {
     int[] myIntArray;               
     myIntArray = new int[4];        
     for (int i = 0; i < 4; i++)     
        myIntArray[i] = i * 10;
     for (int i = 0; i < 4; i++)     
        Console.WriteLine(i);
  }

}</source>

0
1
2
3

Declaring and Accessing an Array

<source lang="csharp">class MainClass {

 static void Main()
 {
   string[] languages = new string[9]{"C#", "COBOL", "Java","C++", "Visual Basic", "Pascal"};
   // Retrieve 5th item in languages array (Visual Basic)
   string language = languages[4];
 }

}</source>

Initializing Anonymous Type Arrays

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; class Program {

 static void Main()
 {
         var worldCup2006Finalists = new[]{new
         {
             TeamName = "France",
             Players = new string[]
             {
                 "A", "B","C", "D",
             }
         },
         new
         {
             TeamName = "Italy",
             Players = new string[]
             {
                 "Q", "W","E", "R",
             }
         }
     };
     Print(worldCup2006Finalists);
 }
 private static void Print<T>(IEnumerable<T> items)
 {
     foreach (T item in items)
     {
         Console.WriteLine(item);
     }
 }

}</source>

Methods Defined by Array

<source lang="csharp">public static int BinarySearch(Array a, object v)</source>

System.Array is a base class for all arrays in C#

Properties Defined by Array

Property Meaning public virtual bool IsFixedSize Is the array fixed size or dynamic.(read-only property) public virtual bool IsReadOnly Is the array read-only. (read-only property). public virtual bool IsSynchronized Is the array safe for use in a multithreaded environment. (read-only property) public int Length The number of elements in the array. (read-only property) public int Rank The dimensions in the array. (read-only property) public virtual object SyncRoot A read-only property that contains the object that must be used to synchronize access to the array.

Use for each loop to output elements in an array

<source lang="csharp">using System; class MainClass {

   public static void Main()
   {
       string s = "A B C D E";
       char[] separators = {" "};
       string[] words = s.Split(separators);
       
       foreach (object obj in words)
          Console.WriteLine("Word: {0}", obj);
   }

}</source>

Word: A
Word: B
Word: C
Word: D
Word: E