Difference between revisions of "OOP Class7"
(→Array List) |
|||
Line 1: | Line 1: | ||
[[Category:OOP]] | [[Category:OOP]] | ||
− | ==Array List== | + | ==Fancy Arrays== |
+ | |||
+ | Arrays | ||
+ | :Regular Array objects are like list collections | ||
+ | |||
+ | Arrays are groups of variables of the same type | ||
+ | :Syntax | ||
+ | <pre> | ||
+ | type [] identifier | ||
+ | </pre> | ||
+ | |||
+ | single dimension arrays | ||
+ | |||
+ | <csharp>string [] aryNames = new string[5]; | ||
+ | |||
+ | aryNames [0] = "Feta"; | ||
+ | aryNames [1] = "Colby"; | ||
+ | aryNames [2] = "Gruyere"; | ||
+ | aryNames [3] = "Edam"; | ||
+ | aryNames [4] = "Colby";</csharp> | ||
+ | |||
+ | Arrays are actually objects and support various methods. | ||
+ | |||
+ | * GetValue | ||
+ | * IndexOf | ||
+ | * LastIndexOf | ||
+ | * SetValue | ||
+ | * Reverse | ||
+ | * Sort | ||
+ | |||
+ | |||
+ | GetValue | ||
+ | |||
+ | <csharp>arrayGetValue.InnerHtml += aryNames.GetValue(1).ToString();</csharp> | ||
+ | :=="Colby" | ||
+ | |||
+ | IndexOf | ||
+ | |||
+ | |||
+ | <csharp>arrayIndexOf.InnerHtml += Array.IndexOf(aryNames, "Colby");</csharp> | ||
+ | :==1 | ||
+ | |||
+ | LastIndexOf | ||
+ | |||
+ | <csharp>arrayLastIndexOf.InnerHtml += Array.LastIndexOf(aryNames, "Colby");</csharp> | ||
+ | :==4 | ||
+ | |||
+ | SetValue | ||
+ | <csharp> | ||
+ | aryNames.SetValue("Gouda", 1); | ||
+ | arraySetValue.InnerHtml += aryNames.GetValue(1).ToString();</csharp> | ||
+ | Now Array(1) == "Gouda" | ||
+ | |||
+ | Reverse | ||
+ | :Reverse reverses the array | ||
+ | <csharp>Array.Reverse(aryNames);</csharp> | ||
+ | |||
+ | The Reversed array looks like Reverse | ||
+ | <pre> | ||
+ | 0 Colby | ||
+ | 1 Edam | ||
+ | 2 Gruyere | ||
+ | 3 Gouda | ||
+ | 4 Feta | ||
+ | </pre> | ||
+ | |||
+ | Sort | ||
+ | :The Sort method sort the array. Arrays are sorted accourding to how objects compare to each other usually implemented using the IComparer interface | ||
+ | |||
+ | <csharp>Array.Sort(aryNames);</csharp> | ||
+ | |||
+ | The Sorted Array List Looks like | ||
+ | <pre> | ||
+ | 0 Colby | ||
+ | 1 Edam | ||
+ | 2 Feta | ||
+ | 3 Gouda | ||
+ | 4 Gruyere | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | Pros and Cons of working with arrays pros | ||
+ | |||
+ | * East to use | ||
+ | * Fast to alter | ||
+ | * Flast to iterate though | ||
+ | * Specific type of elements ( single type) | ||
+ | |||
+ | cons | ||
+ | |||
+ | * Fixed size | ||
+ | * Insterting is difficult | ||
+ | |||
+ | ===Collections=== | ||
+ | |||
+ | Collections are groups of objects. | ||
+ | types of collections | ||
+ | |||
+ | * set - no index | ||
+ | * list - has index | ||
+ | * map - has key/value pair | ||
+ | |||
+ | Set is like storing a bunch of objects in a bag. The order they are removed from the bag has nothing to do with the order they are removed. Set collections do not have a unique identifier. | ||
+ | |||
+ | List types of collections have an integer for an indexer. This gives them a unique identifier and an order. | ||
+ | |||
+ | Map types also have an index but it is not necessarily an integer. It could be anything unique. | ||
+ | |||
+ | ===ArrayList=== | ||
+ | create an arraylist | ||
+ | |||
+ | <csharp>ArrayList myArrayList = new ArrayList();</csharp> | ||
+ | |||
+ | add and element or two | ||
+ | <csharp> | ||
+ | myArrayList.Add("Feta"); | ||
+ | myArrayList.Add("Colby"); | ||
+ | myArrayList.Add("Gruyere"); | ||
+ | myArrayList.Add("Edam"); | ||
+ | myArrayList.Add("Colby"); | ||
+ | </csharp> | ||
+ | This produces and Array list like | ||
+ | <pre> | ||
+ | Feta | ||
+ | Colby | ||
+ | Gruyere | ||
+ | Edam | ||
+ | Colby | ||
+ | </pre> | ||
+ | |||
+ | Insert an element | ||
+ | |||
+ | <csharp>myArrayList.Insert(1,"Gouda");</csharp> | ||
+ | |||
+ | Now our ArrayList Looks like | ||
+ | <pre> | ||
+ | Feta | ||
+ | Gouda | ||
+ | Colby | ||
+ | Gruyere | ||
+ | Edam | ||
+ | Colby | ||
+ | </pre> | ||
+ | |||
+ | Remove an element | ||
+ | |||
+ | <csharp>//remove by index | ||
+ | myArrayList.RemoveAt(1); | ||
+ | //remove by data | ||
+ | myArrayList.Remove("Colby"); //removes first occurance of Colby | ||
+ | </csharp> | ||
+ | |||
+ | Now our Array List looks like | ||
+ | <pre> | ||
+ | Feta | ||
+ | Gruyere | ||
+ | Edam | ||
+ | Colby</pre> | ||
+ | |||
+ | Pros and con of ArrayList | ||
+ | Pros | ||
+ | |||
+ | * No fixed size | ||
+ | * Insert and Remove | ||
+ | |||
+ | Cons | ||
+ | |||
+ | * Slower than arrays | ||
+ | * Inefficient with memory (when the array size exceeds the upper bound .NET doubles the size of the array. | ||
==Polymorphism== | ==Polymorphism== |
Revision as of 17:07, 8 March 2006
Contents
Fancy Arrays
Arrays
- Regular Array objects are like list collections
Arrays are groups of variables of the same type
- Syntax
type [] identifier
single dimension arrays
<csharp>string [] aryNames = new string[5];
aryNames [0] = "Feta"; aryNames [1] = "Colby"; aryNames [2] = "Gruyere"; aryNames [3] = "Edam"; aryNames [4] = "Colby";</csharp>
Arrays are actually objects and support various methods.
- GetValue
- IndexOf
- LastIndexOf
- SetValue
- Reverse
- Sort
GetValue
<csharp>arrayGetValue.InnerHtml += aryNames.GetValue(1).ToString();</csharp>
- =="Colby"
IndexOf
<csharp>arrayIndexOf.InnerHtml += Array.IndexOf(aryNames, "Colby");</csharp>
- ==1
LastIndexOf
<csharp>arrayLastIndexOf.InnerHtml += Array.LastIndexOf(aryNames, "Colby");</csharp>
- ==4
SetValue <csharp>
aryNames.SetValue("Gouda", 1); arraySetValue.InnerHtml += aryNames.GetValue(1).ToString();</csharp>
Now Array(1) == "Gouda"
Reverse
- Reverse reverses the array
<csharp>Array.Reverse(aryNames);</csharp>
The Reversed array looks like Reverse
0 Colby 1 Edam 2 Gruyere 3 Gouda 4 Feta
Sort
- The Sort method sort the array. Arrays are sorted accourding to how objects compare to each other usually implemented using the IComparer interface
<csharp>Array.Sort(aryNames);</csharp>
The Sorted Array List Looks like
0 Colby 1 Edam 2 Feta 3 Gouda 4 Gruyere
Pros and Cons of working with arrays pros
- East to use
- Fast to alter
- Flast to iterate though
- Specific type of elements ( single type)
cons
- Fixed size
- Insterting is difficult
Collections
Collections are groups of objects. types of collections
* set - no index * list - has index * map - has key/value pair
Set is like storing a bunch of objects in a bag. The order they are removed from the bag has nothing to do with the order they are removed. Set collections do not have a unique identifier.
List types of collections have an integer for an indexer. This gives them a unique identifier and an order.
Map types also have an index but it is not necessarily an integer. It could be anything unique.
ArrayList
create an arraylist
<csharp>ArrayList myArrayList = new ArrayList();</csharp>
add and element or two <csharp> myArrayList.Add("Feta"); myArrayList.Add("Colby"); myArrayList.Add("Gruyere"); myArrayList.Add("Edam"); myArrayList.Add("Colby"); </csharp> This produces and Array list like
Feta Colby Gruyere Edam Colby
Insert an element
<csharp>myArrayList.Insert(1,"Gouda");</csharp>
Now our ArrayList Looks like
Feta Gouda Colby Gruyere Edam Colby
Remove an element
<csharp>//remove by index myArrayList.RemoveAt(1); //remove by data myArrayList.Remove("Colby"); //removes first occurance of Colby </csharp>
Now our Array List looks like
Feta Gruyere Edam Colby
Pros and con of ArrayList Pros
- No fixed size
- Insert and Remove
Cons
- Slower than arrays
- Inefficient with memory (when the array size exceeds the upper bound .NET doubles the size of the array.