Visual C++ .NET/Collections/ArrayList
Содержание
- 1 Adding strings to ArrayList
- 2 Adding to the end of an ArrayList
- 3 Adding value to ArrayList with Indexer
- 4 Binary search an ArrayList by functor
- 5 Does an ArrayList contain a value
- 6 Get the capacity of an ArrayList
- 7 Get the element count
- 8 Insert value to an ArrayList at specified position
- 9 Iterate ArrayList using indexing
- 10 Iterate ArrayList using the for each operator
- 11 Reverse the values in an ArrayList
- 12 Set the capacity
- 13 Sort an ArrayList
- 14 Sort an ArrayList by functor
- 15 Using AddRange to add an array of value to ArrayList
- 16 Using for loop the display elements in an ArrayList
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]);
}
}