Csharp/CSharp Tutorial/Data Structure/Array — различия между версиями

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

Текущая версия на 12:15, 26 мая 2010

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:


type[ ] array-name = new type[size];

Array Declaration with initialization

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


type[ ] array-name = { val1, val2, val3, ..., valN };

Array Inherited Members: get array type

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

Array Inherited Members: GetLength(0)

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

Array Inherited Members: Rank and Length

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);
   }
}
Rank = 1, Length = 5

Declare the array and instantiate the array

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);
   }
}
0
1
2
3

Declaring and Accessing an Array

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];
  }
}

Initializing Anonymous Type Arrays

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);
      }
  }
}

Methods Defined by Array

public static int BinarySearch(Array a, object v)

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

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);
    }
}
Word: A
Word: B
Word: C
Word: D
Word: E