Visual C++ .NET/Collections/ArrayList

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

Adding strings to ArrayList

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


Adding to the end of an ArrayList

 
#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";

}


Adding value to ArrayList with Indexer

 
#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";

}


Binary search an ArrayList by functor

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


Does an ArrayList contain a value

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


Get the capacity of an ArrayList

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


Get the element count

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


Insert value to an ArrayList at specified position

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


Iterate ArrayList using indexing

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


Iterate ArrayList using the for each operator

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


Reverse the values in an ArrayList

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


Set the capacity

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


Sort an ArrayList

 
#include "stdafx.h"
#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();
}


Sort an ArrayList by functor

 

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


Using AddRange to add an array of value to ArrayList

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


Using for loop the display elements in an ArrayList

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