70 likes | 158 Views
Arrays and Collections. Arrays One dimensional array Two or multiple dimensional array Collections Standard collection: ArrayList Generic collection: LinkedList. Array in C++/CLI. Array, element, index(or subscript) array<int> ^ a = gcnew array<int>(12);
E N D
Arrays and Collections • Arrays • One dimensional array • Two or multiple dimensional array • Collections • Standard collection: ArrayList • Generic collection: LinkedList
Array in C++/CLI • Array, element, index(or subscript) • array<int> ^ a = gcnew array<int>(12); • array<int> ^ a = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; • a[0], a[1],…, and a[11] • a->Length • array<String ^> ^d = {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”}; • int main(array<System::String ^> ^args) {}
Array in C++/CLI • Multi-dimensional Array • array<int, 2> ^ b = gcnew array<int, 2>(4,3); • array<int, 2> ^ b = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0, 1, 2}}; • b[0,0], b[0,1],…, b[3,2]; • b->Rank, b->GetLength(0), b->GetLength(1)
Collections in C++/CLI • ArrayList(System::Collections Namespace) • Index-based • Constant-time random access • consecutive • Dynamically growing arrays, re-sizeable • LinkedList(System::Collections::Generic Namespace) • Traverse the list to find the proper node • Dis-contiguous
Collections in C++/CLI • ArrayList ^ list1 = gcnew ArrayList(); • Property: Count • Methods: [], Add, Insert, RemoveAt, Clear
Collections in C++/CLI • LinkedList<String^> ^list2 = gcnew LinkedList<String^>(); • LinkedListNode<String^> ^cur; cur->Value, cur->Next; cur->Previous • Property: Count, First, Last • Methods: AddFirst, AddLast, AddBefore, AddAfter, RemoveFirst, RemoveLast, Remove, Clear
Exercise • Create an array with fixed length containing “Monday”, “Tuesday”,…….“Sunday”, and print the array. • Create an array which is able to hold arbitrary number of student names, add n names and finally print. • ArrayList • LinkedList