1 / 8

Array Lists

Array Lists. pg. 234-239. Arrays in C#. Can set size at run time (as in C++) int actualSize = …; Emp [ ] staff = new Emp [actualSize]; Does not completely resolve problem of dynamically modifying arrays – once array size is set, cannot change easily. ArrayList class in C#.

ayame
Télécharger la présentation

Array Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Lists pg. 234-239

  2. Arrays in C# • Can set size at run time (as in C++) int actualSize = …; Emp [ ] staff = new Emp [actualSize]; • Does not completely resolve problem of dynamically modifying arrays – once array size is set, cannot change easily.

  3. ArrayList class in C# • Untyped collections defined in System.Collections package • Typed collections in System.Collections.Generic package • Shrink and grow dynamically • Lower bound is 0 • Holds references to objects • Need to cast whenever you take an item out of an typed array list

  4. add Method ArrayList staff = new ArrayList( ); staff.add (new Emp ( …) ); staff.add (new Emp (…) ); • ArrayList class manages internal array of object references. • When it gets full, then the array list automatically creates a bigger array and copies objects in smaller array to bigger array.

  5. Size stuff • Returns actual number of elements in array list staff.Count • When you know it has reached permanent size staff.TrimToSize ( );

  6. Accessing Array List elements • To get ith element (object),: staff [i] • To get ith element (if typed) , must cast employee e = (Emp) staff [ i];

  7. ArrayList Trick • Flexible growth and convenient element access • First make an array list and add elements ArrayList list = new ArrayList ( ); while ( …) { x = …; list.add (x); } • Then use ToArray method to copy elements into array.

  8. ArrayList Trick • Then use ToArray method to copy elements into array (untyped)   int[] a = (int[]) list.ToArray(typeof(int));  • Other convenient methods • Contains (object) • BinarySearch (object) • Sort( )

More Related