Csharp/CSharp Tutorial/Data Structure/Array Index

Материал из .Net Framework эксперт
Версия от 12:15, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Assigning array reference variables

using System; 
 
class MainClass {  
  public static void Main() {  
    int i; 
 
    int[] nums1 = new int[10]; 
    int[] nums2 = new int[10]; 
 
    for(i=0; i < 10; i++) 
       nums1[i] = i; 
 
    for(i=0; i < 10; i++) 
       nums2[i] = -i; 
 
    Console.Write("Here is nums1: "); 
    for(i=0; i < 10; i++) 
      Console.Write(nums1[i] + " ");   
    Console.Write("Here is nums2: "); 
    for(i=0; i < 10; i++) 
      Console.Write(nums2[i] + " ");   
    Console.WriteLine(); 
 
    nums2 = nums1; // now nums2 refers to nums1 
 
    Console.Write("Here is nums2 after assignment: "); 
    for(i=0; i < 10; i++) 
      Console.Write(nums2[i] + " ");   
    Console.WriteLine(); 
  }  
}
Here is nums1: 0 1 2 3 4 5 6 7 8 9 Here is nums2: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9
Here is nums2 after assignment: 0 1 2 3 4 5 6 7 8 9

Demonstrate an array overrun

using System; 
 
class MainClass {  
  public static void Main() {  
    int[] sample = new int[10]; 
    int i;  
  
    // generate an array overrun 
    for(i = 0; i < 100; i = i+1)  
      sample[i] = i; 
  }  
}
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at MainClass.Main()

Set array element value by index(subscript)

using System; 
 
class MainClass {  
  public static void Main() {  
    int[] nums = new int[10]; 
    int avg = 0; 
 
    nums[0] = 99; 
    nums[1] = 10; 
    nums[2] = 100; 
    nums[3] = 18; 
    nums[4] = 78; 
    nums[5] = 23; 
    nums[6] = 63; 
    nums[7] = 9; 
    nums[8] = 87; 
    nums[9] = 49; 
 
    for(int i=0; i < 10; i++)  
      avg = avg + nums[i]; 
 
    avg = avg / 10; 
 
    Console.WriteLine("Average: " + avg); 
  }  
}
Average: 53

Swapping Data between Positions in an Array

class MainClass
{
  static void Main()
  {
    string[] languages = new string[9]{"C#", "COBOL", "Java","C++"};
    string language = languages[3];
    languages[3] = languages[2];
    languages[2] = language;
  }
}

Use the IndexOf() and LastIndexOf() methods to find the string "Hello" in stringArray

using System;
class MainClass
{
  public static void Main()
  {
    string[] stringArray = {"Hello", "to", "everyone", "Hello", "all"};
    Console.WriteLine("stringArray:");
    for (int i = 0; i < stringArray.Length; i++)
    {
      Console.WriteLine("stringArray[" + i + "] = " + stringArray[i]);
    }

    int index = Array.IndexOf(stringArray, "Hello");
    Console.WriteLine("Array.IndexOf(stringArray, \"Hello\") = " + index);
    index = Array.LastIndexOf(stringArray, "Hello");
    Console.WriteLine("Array.LastIndexOf(stringArray, \"Hello\") = " + index);
  }
}
stringArray:
stringArray[0] = Hello
stringArray[1] = to
stringArray[2] = everyone
stringArray[3] = Hello
stringArray[4] = all
Array.IndexOf(stringArray, "Hello") = 0
Array.LastIndexOf(stringArray, "Hello") = 3

Use the IndexOf() and LastIndexOf() methods to find the value 1 in intArray

using System;
class MainClass
{
  public static void Main()
  {
    int[] intArray = {1, 2, 1, 3};
    Console.WriteLine("intArray:");
    for (int i = 0; i < intArray.Length; i++)
    {
      Console.WriteLine("intArray[" + i + "] = " +
        intArray[i]);
    }
    
    int index = Array.IndexOf(intArray, 1);
    Console.WriteLine("Array.IndexOf(intArray, 1) = " + index);
    index = Array.LastIndexOf(intArray, 1);
    Console.WriteLine("Array.LastIndexOf(intArray, 1) = " + index);
  }
}
intArray:
intArray[0] = 1
intArray[1] = 2
intArray[2] = 1
intArray[3] = 3
Array.IndexOf(intArray, 1) = 0
Array.LastIndexOf(intArray, 1) = 2