Encase数据结构之ArrayClass

1,概述

ArrayClass是一个任何对象容器。它并不是一个类而是类似于编程语言中的关键字typedef(别名)。

它的本质类似于

typedef String[] StringArray

ArrayClass可以根据需要通过SetCount()来分配内存。

下面有两种方法用来构造数组

一是通过使用构造函数,另一种是显示声明,如array {"asd","cds","vfd","fdg"}

 

2,ArrayClass中的枚举类型

 

Name Value Description
SORTENABLED128排序(升序)
SORTDESCENDING256降序排列
SORTCASE512条件排序(如有大小写)
SORTNODUPE1024去重

3,ArrayClass中的方法

Name Return Type Declaration Description
ArrayClassvoidArrayClass (uint Count=0,uint Size=0)Array Constructor
Arguments:
count - the number of items to intialize
size - the number of items to allocate memory
AddvoidAdd (void Value)Adds an item to this array, or else a Compile Error will occurr.
Arguments:
value - must be of the same type as the other objects in this array
if array does not have enough allocated space, the Add() method wil allocate more memory for the new item.
Countuintconst Count ()Returns the number (as an unsigned integer) of items in this array -- not the memory size of this array.
DeleteuintDelete (uint index,uint Count=1)Delete items from this array.
Arguments:
index - unsigned integer zero-based index of item to delete
count - unsigned integer number of items to delete after (and including) index. If count is zero, then no objects will be deleted.
DynamicTypeSymbolClassconst DynamicType ()Returns the symbol object representing the dynamic type of this object. Symbol objects contain class names, method names and properties
SetCountvoidSetCount (uint Value)Sets a new maximum size for the array
Arguments:
Value - New size of the array
TypeCastArrayClassstatic TypeCast (ObjectClass Value)Casts value to this type. Returns null if value is not of type
~ArrayClassvoid~ArrayClass ()Destructor - deletes the object from memory. This method cannot be called directly. Encase will call it whenever there are no more references to this object

4,Array条件方法

Name Return Type Declaration Description
SortvoidSort (uint options=ArrayClass::SORTENABLED)Sorts the array.
'options' are ArrayClass::SortOptions::SORTENABLED,
ArrayClass::SortOptions::SORTDESCENDING,
ArrayClass::SortOptions::SORTCASE,
ArrayClass::SortOptions::SORTNODUPE
FindintFind (void value, uint options = 0, uint start = 0, length = -1)Returns the index of the first matching value, or -1 if not found.
'value' is value to search for.
'options' are {class}::FindOptions::CASE (e.g. String::FindOptions::CASE).
'start' is index where search should begin.
'length' is maximum number of indexes to search.

5,范例

范例1:创建一个整型数组,输出它的内容,然后删除其中3-7,再次打印数组内容

class MainClass {

  typedef int[] IntArray;

  void Main() {
    //bool worked;
    int notWorks;
    IntArray arr(0, 15), //create an empty array with at most 15 items
             testArr(0, 5);
    testArr.Add(0); // adding the elements to the testArr array
    testArr.Add(1);
    testArr.Add(2);
    testArr.Add(8);
    testArr.Add(9);
    for (int i = 0; i < 10; i++) {
      arr.Add(i);
    }
    //print array contents
    int cnt = arr.Count(); //count() returns # of objects not MAX size
    for (int i = 0; i < cnt; i++) {
      Console.Write("\t\t" + arr[i]);
      if (arr[i] != i)
        notWorks++;
    }
    Console.WriteLine("");
    //delete items 3-7
    arr.Delete(3, 5);
    //print array objects
    cnt = arr.Count();
    for (int i = 0; i < cnt; i++) {
      Console.Write("\t\t" + arr[i]);
      if (testArr[i] != arr[i])
        notWorks++;
    }
    if (notWorks == 0)
      Console.WriteLine("\nWorked");
    else
      Console.WriteLine("\n Does not Work");
  }
}


输出结果

  0  1  2  3  4  5  6  7  8  9
  0  1  2  8  9
Worked

范例2:创建一个对象数组

class AClass {

  String Name;
  AClass(const String &s = ""):
    Name = s
  {
  }
}

class MainClass {
  typedef AClass[] ClassArray;
  typedef int[] IntArray;
  void Main() {
    ClassArray arrClass {    }; //create an empty array
    IntArray testArr(0, 5);
    testArr.Add(0); // adding the elements to the testArr array
    testArr.Add(1);
    testArr.Add(2);
    testArr.Add(8);
    testArr.Add(9);
    int notWorks;
    for (int i = 0; i < 10; i++)
      arrClass.Add(new AClass(i));
    //print array contents
    int cnt = arrClass.Count(); //count() returns # of objects not MAX size
    for (int i = 0; i < cnt; i++) {
      Console.Write("\t\t" + arrClass[i].Name);
      if (arrClass[i].Name != i)
        notWorks++;
    }
    Console.WriteLine("");
    //delete items 3-7
    arrClass.Delete(3, 5);
    //print array objects
    cnt = arrClass.Count();
    for (int i = 0; i < cnt; i++) {
      Console.Write("\t\t" + arrClass[i].Name);
      if (arrClass[i].Name != testArr[i])
        notWorks++;
    }
    if (notWorks == 0)
      Console.WriteLine("\nWorked");
    else
      Console.WriteLine("\n Does not Work");
  }
}

输出结果

  0  1  2  3  4  5  6  7  8  9
  0  1  2  8  9
Worked

范例3:多维数组

class MainClass {
  typedef int[]      IntArray;
  typedef IntArray[] Matrix;

  void Main() {
    IntArray testArr(0, 6),
             test2Arr(0, 6);
    testArr.Add(1); // adding the elements to the testArr array
    testArr.Add(0);
    testArr.Add(0);
    testArr.Add(0);
    testArr.Add(0);
    testArr.Add(1);
    int      topIndex = 0,
             secongIndex = 0,
             notWorks = 0;
    Matrix m(2);
    foreach (IntArray a in m)
      a = new IntArray(3);

    m[0][0] = 1; // only initialising m[0][0] and m[1][2] to "1". So the other elements should be "0"
    m[1][2] = 1;

    foreach (IntArray a in m) {
      secongIndex = 0;
      foreach (int i in a) {
        Console.WriteLine("\tm["+topIndex+"]["+secongIndex+"] = "+i);
        test2Arr.Add(i);
        secongIndex++;
      }
      topIndex++;
    }
    for(int index = 0; index < 6; index++) {
      if (testArr[index] != test2Arr[index])
        notWorks++;
    }
    if (notWorks == 0)
      Console.WriteLine("Worked");
    else
      Console.WriteLine("Does not Work");
  }
}

输出结果

 m[0][0] = 1
 m[0][1] = 0
 m[0][2] = 0
 m[1][0] = 0
 m[1][1] = 0
 m[1][2] = 1
Worked

范例4:排序方法

class MainClass {
  typedef String[] StringArray;
  void Show(const String &name,StringArray a) {
    Console.Write(name + ": ");
    foreach (String s in a)
      Console.Write(s);
    Console.WriteLine();
  }
  void Main() {
    SystemClass::ClearConsole(SystemClass::SHOWCONSOLE);
    StringArray a1 {"c","F","B","D","A","H","K","e","J","i","g"},b1();
    Show("Original",a1);

    // Add strings in a to b in sorted order
    b1.Sort(SORTENABLED);
    foreach (String s in a1)
      b1.Add(s);
    Show("Ascending",b1);

    // Resort array in descending order
    b1.Sort(SORTENABLED | SORTDESCENDING);
    Show("Descending",b1);

    // Resort array with case sensitive comparison
    b1.Sort(SORTENABLED | SORTCASE);
    Show("Case Sensitive",b1);

    // Add strings in a to b in sorted order,removing duplicate values
    StringArray a2 {"a","a","a","b","b","b","b","c","c","c","c"},b2();
    b2.Sort(SORTENABLED | SORTNODUPE);
    foreach (String s in a2)
      b2.Add(s);
    Show("De-Duped",b2);
  }
}

输出结果

Original: cFBDAHKeJig
Ascending: ABcDeFgHiJK
Descending: KJiHgFeDcBA
Case Sensitive: ABDFHJKcegi
De-Duped: abc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值