Monday, August 8, 2016

Collections

Collection classes are specialized classes for data storage and retrieval.
Allocate memory dynamically to elements and accessing a list of items on the basis of an index.

Array
  • Fixed Length 
  • Strongly Typed -> Developers determine its type at the instantiation time instead of runtime. This feature makes to run fast at runtime, since it does not need to wait for type definition. 
  • Fixed Length and Strongly Typed consume less memory, therefore it has good performance.
List

  • List is NOT Fixed Length
  • List is Strongly Typed

Not Fixed Length makes developers feel flexible to use it, and because of it is Strongly Typed when it is defined "Generic" so our code runs fast at runtime because it does not need to wait for type definition.

ArrayList
  • Represents ordered collection of an object that can be indexed individually.
  • NOT Fixed Length
  • NOT Strongly Typed -> Whenever developers are not sure about what is exactly type definition for input or output data.
  • Time consuming at the runtime for memory to determine type definition.
  • can add and remove items from a list at a specified position using an index and the array resizes itself automatically (Array cannot). 
  • Allows dynamic memory allocation, adding, searching and sorting items in the list.
ArrayList VS List

ArrayList will get runtime error when casting, List will get compile error

Hashtable
  • It uses a key to access the elements in the collection.
  • Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
  • NOT strongly typed 
  • NOT fixed size
ht.Add("001", "Zara Ali");
SortedList
  • It uses a key or index to access the items in a list.
  • A sorted list is a combination of an array and a hash table.
  • If you access items using an index, it is an ArrayList, if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value.
Stack
  • It represents a last-in, first out collection of object.
  • When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.
st.Push('A');st.Push('B');st.Push('C');st.Push('D');
st.Push('E');st.Push('F');
Current stack: D B A
ADD H stack: F E D C B A
Call 3 times POP Remove : C B A

Queue
  • It represents a first-in, first out collection of object.
  • When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.
q.Enqueue('A');q.Enqueue('B');q.Enqueue('C');q.Enqueue('D');
q.Enqueue('E');q.Enqueue('F');
Current queue: A B C D 
ADD VH queue: A B C D E F 
Removing values
The removed value: A
The removed value: B

BitArray
  • It represents an array of the binary representation using the values 1 and 0.
  • It is used when you need to store the bits but do not know the number of bits in advance.
  • using an integer index, which starts from zero.

No comments:

Post a Comment