Arrays and Collections Arrays One dimensional array Two

  • Slides: 7
Download presentation
Arrays and Collections • Arrays – One dimensional array – Two or multiple dimensional

Arrays and Collections • Arrays – One dimensional array – Two or multiple dimensional array • Collections – Standard collection: Array. List – Generic collection: Linked. List

Array in C++/CLI • Array, element, index(or subscript) • array<int> ^ a = gcnew

Array in C++/CLI • Array, element, index(or subscript) • array<int> ^ a = gcnew array<int>(12); • array<int> ^ a = {31, 28, 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,

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->Get. Length(0), b->Get. Length(1)

Collections in C++/CLI • Array. List (System: : Collections Namespace) – Index-based – Constant-time

Collections in C++/CLI • Array. List (System: : Collections Namespace) – Index-based – Constant-time random access – consecutive – Dynamically growing arrays, re-sizeable • Linked. List (System: : Collections: : Generic Namespace) – Traverse the list to find the proper node – Dis-contiguous

Collections in C++/CLI • Array. List ^ list 1 = gcnew Array. List(); •

Collections in C++/CLI • Array. List ^ list 1 = gcnew Array. List(); • Property: Count • Methods: [], Add, Insert, Remove. At, Clear

Collections in C++/CLI • Linked. List<String^> ^list 2 = gcnew Linked. List<String^>(); • Linked.

Collections in C++/CLI • Linked. List<String^> ^list 2 = gcnew Linked. List<String^>(); • Linked. List. Node<String^> ^cur; cur->Value, cur->Next; cur->Previous • Property: Count, First, Last • Methods: Add. First, Add. Last, Add. Before, Add. After, Remove. First, Remove. Last, Remove, Clear

Exercise • Create an array with fixed length containing “Monday”, “Tuesday”, ……. “Sunday”, and

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. – Array. List – Linked. List