Visual C++ .NET/Collections/ArrayList

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

Adding strings to ArrayList

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections; int main() {

  ArrayList^ array_list = gcnew ArrayList();
  array_list->Add("apple");
  array_list->Add("banana");
  for each (String^ s in array_list)
  {
      Console::WriteLine( s );
  }
  for (int i = 0; i < array_list->Count; i++)
  {
      Console::WriteLine("{0} {1}", i, array_list[i]);
  }

}

 </source>


Adding to the end of an ArrayList

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   alist[1] = "Three";

}

 </source>


Adding value to ArrayList with Indexer

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   alist[1] = "Three";

}

 </source>


Binary search an ArrayList by functor

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections; ref class myReverserClass: public IComparer { public:

  virtual int Compare(Object^ x, Object^ y) = IComparer::Compare
  {
      return (String::Compare((String^)y, (String^)x));
  }

}; void main() {

   ArrayList ^alist = gcnew ArrayList(4);
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   
   IComparer^ myComparer = gcnew myReverserClass;
   alist->Sort(myComparer);
   
   int indx = alist->BinarySearch("Four", myComparer);
   Console::WriteLine("Four found at index: {0}", indx.ToString());
   

}

 </source>


Does an ArrayList contain a value

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   Console::WriteLine("\n\nCapacity is: {0}", alist->Capacity.ToString());
   alist->Capacity = 10;
   
   Console::WriteLine("New capacity is: {0}", alist->Capacity.ToString());
   
   
   bool fnd = alist->Contains("One");
   Console::WriteLine("ArrayList contains a "One": {0}", fnd.ToString());
   

}

 </source>


Get the capacity of an ArrayList

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   Console::WriteLine("\n\nCapacity is: {0}", alist->Capacity.ToString());

}

 </source>


Get the element count

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   Console::WriteLine("\n\nCapacity is: {0}", alist->Capacity.ToString());
   alist->Capacity = 10;
   
   Console::WriteLine("New capacity is: {0}", alist->Capacity.ToString());
   
   
   Console::WriteLine("Count is: {0}", alist->Count.ToString());
   

}

 </source>


Insert value to an ArrayList at specified position

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   alist[1] = "Three";
   alist->Insert(1, "Two");

}

 </source>


Iterate ArrayList using indexing

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections; int main() {

  ArrayList^ array_list = gcnew ArrayList();
  array_list->Add("apple");
  array_list->Add("banana");
  for each (String^ s in array_list)
  {
      Console::WriteLine( s );
  }
  for (int i = 0; i < array_list->Count; i++)
  {
      Console::WriteLine("{0} {1}", i, array_list[i]);
  }

}

 </source>


Iterate ArrayList using the for each operator

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections; int main() {

  ArrayList^ array_list = gcnew ArrayList();
  array_list->Add("apple");
  array_list->Add("banana");
  for each (String^ s in array_list)
  {
      Console::WriteLine( s );
  }
  for (int i = 0; i < array_list->Count; i++)
  {
      Console::WriteLine("{0} {1}", i, array_list[i]);
  }

}

 </source>


Reverse the values in an ArrayList

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   alist->Reverse();

}

 </source>


Set the capacity

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   Console::WriteLine("\n\nCapacity is: {0}", alist->Capacity.ToString());
   alist->Capacity = 10;
   
   Console::WriteLine("New capacity is: {0}", alist->Capacity.ToString());

}

 </source>


Sort an ArrayList

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringDictionary ^strdict = gcnew StringDictionary();
   strdict->Add("D", "d");
   strdict->Add("F", "f");
   strdict["C"] = "c";
   
   ArrayList ^alist = gcnew ArrayList();
   alist->AddRange(strdict->Keys);
   alist->Sort();

}

 </source>


Sort an ArrayList by functor

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections; ref class myReverserClass: public IComparer { public:

  virtual int Compare(Object^ x, Object^ y) = IComparer::Compare
  {
      return (String::Compare((String^)y, (String^)x));
  }

}; void main() {

   ArrayList ^alist = gcnew ArrayList(4);
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   
   IComparer^ myComparer = gcnew myReverserClass;
   alist->Sort(myComparer);
   
   int indx = alist->BinarySearch("Four", myComparer);
   Console::WriteLine("Four found at index: {0}", indx.ToString());
   

}

 </source>


Using AddRange to add an array of value to ArrayList

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);

}

 </source>


Using for loop the display elements in an ArrayList

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections;

void main() {

   ArrayList ^alist = gcnew ArrayList(4); // will double to 8
   alist->Add("One");
   alist->Add("-");
   array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};
   alist->AddRange(morenums);
   for (int i = 0; i < alist->Count; i++)
   {
       Console::Write("{0} ", alist[i]);
   }

}

 </source>