Presentation is loading. Please wait.

Presentation is loading. Please wait.

(H9.1) CirkelKlikker ARRAY int x, y; [ ] int n=0;

Similar presentations


Presentation on theme: "(H9.1) CirkelKlikker ARRAY int x, y; [ ] int n=0;"— Presentation transcript:

1 (H9.1) CirkelKlikker ARRAY int x, y; [ ] int n=0;
void klik(object o, MouseEA mea) { } this.x = mea.X; this.y = mea.Y; this.Invalidate(); [n] [n] n++; void teken(object o, PaintEA pea) { } Graphics gr = pea.Graphics; ARRAY for (int t=0; t<n; t++) gr.FillEllipse(Brushes.Black, this.x , this.y , 15, 15 ); [t] [t]

2 Arrays Array: rij genummerde variabelen declaratie van een array
tabel 1 2 3 4 length 5 int [ ] tabel; tabel = new int [5]; creëren van het eigenlijke array-object

3 Twee-dimensionale array
tabel 5 3 length int [ , ] tabel; 1 2 tabel = new int [5,3]; 3 4 tabel.GetLength(0) tabel.GetLength(1) 1 2

4 LetterTeller class LetterTeller : Form TextBox invoer Label uitvoer
StaafDiagram diagram class StaafDiagram : UserControl class TurfTab

5 Letterteller hoofdklasse
class LetterTeller : Form { TextBox invoer; Label uitvoer; StaafDiag diagram; LetterTeller() { invoer = new TextBox(); invoer.MultiLine = true; uitvoer = new Label(); diagram = new StaafDiag(); } invoer . TextChanged += this.Bereken; void Bereken(object o, EventArgs ea) { } TurfTab tabel = new TurfTab(); tabel.Turf ( invoer.Text ); uitvoer.Text = tabel.ToString() ; diagram.Waardes = tabel.Waardes ; }

6 De klasse TurfTab public class TurfTab
{ private int [ ] tellers; private int totaal; public TurfTab ( ) { } tellers = new int [26]; public void Turf (string s) { } for (int t=0; t<s.Length; t++) this.turf ( s[t] ); }

7 De klasse TurfTab public class TurfTab
{ private int [ ] tellers; private int totaal; private void turf (char c) { } if (c>='A' && c<='Z') { } tellers [ ] ++; totaal++; c - 'A' if (c>='a' && c<='z') { tellers [ c - 'a' ] ++; totaal++; } }

8 De klasse TurfTab public class TurfTab
{ private int [ ] tellers; private int totaal; een eigen property! public int Totaal { get { } "mini- methodes" "mini- methodes" return totaal; set { } totaal = value; } }

9 geen set-minimethode:
De klasse TurfTab public class TurfTab { private int [ ] tellers; private int totaal; een eigen property! public int Totaal { get { } "mini- methodes" "mini- methodes" return totaal; geen set-minimethode: read-only property set { } totaal = value; } }

10 …is eigenlijk overbodig!
Array …is eigenlijk overbodig! array: oject dat een rij waarden bevat, met speciale notaties List: array “ingepakt” in een klasse, met standaard notaties uitgebreide Declaratie Creatie Opvragen Wijzigen Lengte String [ ] a; List<String> a; List a; a = new String[10]; a = new List(); a = new List<String>(); ……a[5]…… ……a[5]…… …a.Get(5)… a[5] = ……; a[5] = ……; a.Set(5,…); …a.Length… …a.Count… Invoegen Achtervoegen a.Insert(5,…); a.Add(…);

11 Voorbeeld: array class Voorbeeld : Form { } TextBox in; Button b;
String [] alles; int n; Voorbeeld ( ) { } void teken(object o, PEA pea) { } in = new TextBox(); b = new Button(); b.Click += klik; this.Paint += teken; int y = 50; for (int t=0; t<n; t++) { pea.Graphics.DrawString( alles[t], , y ); y+=20; } alles = new String[100]; n = 0; void klik(object o, AE ae) { } if (n<100) { String s = in.Text; alles[n] = s; n++; this.Invalidate(); }

12 List Voorbeeld: array class Voorbeeld : Form { } TextBox in; Button b;
List<String> alles; String [] alles; int n; Voorbeeld ( ) { } void teken(object o, PEA pea) { } in = new TextBox(); b = new Button(); b.Click += klik; this.Paint += teken; int y = 50; for (int t=0; t<n; t++) { pea.Graphics.DrawString( alles[t], , y ); y+=20; } alles.Count ;t++) alles[t] alles = new List<String>( ); alles = new String[100]; n = 0; void klik(object o, AE ae) { } if (n<100) { String s = in.Text; alles[n] = s; n++; alles.Add(s); this.Invalidate(); }

13 Hoe is List gemaakt? class List<Elem> { } Elem [] elems; int n;
{ elems = new Elem[10]; n = 0; } void Add(Elem e) { } if (n>=elems.Length) vergroot(); elems[n]=e; n++; Elem Get (int p) { return elems[p]; } this [int p] private void vergroot() { } get { } Elem [] kopie; kopie= new Elem[2*n]; for (int t=0; t<n; t++) kopie[t] = elems[t] int Count { get { return n; } } elems = kopie;

14 Kan het ook anders? class List<Elem> { } Elem [] elems; int n;
{ elems = new Elem[10]; n = 0; } void Add(Elem e) { } if (n>=elems.Length) vergroot(); elems[n]=e; n++; Elem Get (int p) { return elems[p]; } this [int p] private void vergroot() { } get { } Elem [] kopie; kopie= new Elem[2*n]; for (int t=0; t<n; t++) kopie[t] = elems[t] int Count { get { return n; } } elems = kopie; n*=2;

15 Ja: je kunt een List maken zonder dat een array nodig is
class AndereList<Elem> { } …… 3 null © AndereList<Elem>() { …… } Elem this[int p] { …… } int Count { …… } void Add(Elem e) { …… }

16 Ja: je kunt een List maken zonder dat een array nodig is
class AndereList<Elem> { } …… null © AndereList<Elem>() { …… } 3 Elem this[int p] { …… } int Count { …… } void Add(Elem e) { …… }

17 Ja: je kunt een List maken zonder dat een array nodig is
class AndereList<Elem> { } : IList interface IList<Elem> { } …… int Count { get; } ; void Clear(); void Add(Elem e) ; Elem this [int p] { get; set; }; void Insert (int p, Elem e); © AndereList<Elem>() { …… } Elem this[int p] { …… } int Count { …… } Klasse implementeert de methodes Interface specificeert de methodes void Add(Elem e) { …… }

18 Namespace System.Collections.Generic
interface diverse implementaties IList List Andere List

19 Voorbeeld: List class Voorbeeld : Form { } TextBox in; Button b;
List<String> alles; IList<String> alles; Voorbeeld ( ) { } void teken(object o, PEA pea) { } in = new TextBox(); b = new Button(); b.Click += klik; this.Paint += teken; int y = 50; for (int t=0; t<n; t++) { pea.Graphics.DrawString( alles[t], , y ); y+=20; } alles.Count ;t++) alles = new List <String>( ); alles[t] alles = new String[100]; n = 0; AndereList void klik(object o, AE ae) { } String s = in.Text; alles[n] = s; n++; alles.Add(s); this.Invalidate();

20 Varianten van List genummerd zonder dubbele
ICollection IList zonder dubbele ISet interface ICollection<Elem> { void Add(Elem x); bool Remove(Elem x); bool Contains(Elem x); int Count { get; }; void Clear(); } interface IList<Elem> : ICollection<Elem> { Elem this[int n] { get; set; } ; int IndexOf(Elem x); void Insert(int n, Elem x); void RemoveAt(int n); }

21 Implementaties van Collections
ICollection LinkedList Queue Stack IList List ISet HashSet SortedSet IDictionary SortedList Sorted Dictionary

22 Voorbeeld: Collection
class Voorbeeld : Form { } TextBox in; Button b; List<String> alles; IList<String> alles; ICollection<String> alles; Voorbeeld ( ) { } void teken(object o, PEA pea) { } in = new TextBox(); b = new Button(); b.Click += klik; this.Paint += teken; int y = 50; for (int t=0; t<n; t++) { pea.Graphics.DrawString( alles[t], , y ); y+=20; } alles.Count ;t++) alles = new List <String>( ); alles[t] alles = new String[100]; n = 0; LinkedList void klik(object o, AE ae) { } String s = in.Text; alles[n] = s; n++; alles.Add(s); this.Invalidate();

23 Hoe doorloop je een Collection?
List Collection for (int t=0; t<alles.Count; t++) doeIetsMet( alles[t] ); foreach (String s in alles) doeIetsMet( s ); opdracht for ) ( expr ; type expr in naam foreach ( ) opdracht

24 Voorbeeld: Collection
class Voorbeeld : Form { } TextBox in; Button b; List<String> alles; IList<String> alles; ICollection<String> alles; Voorbeeld ( ) { } void teken(object o, PEA pea) { } in = new TextBox(); b = new Button(); b.Click += klik; this.Paint += teken; int y = 50; foreach ( String s in alles ) for (int t=0; t<n; t++) { pea.Graphics.DrawString( alles[t], , y ); y+=20; } alles.Count ;t++) alles = new List <String>( ); alles[t] s alles = new String[100]; n = 0; LinkedList void klik(object o, AE ae) { } String s = in.Text; alles[n] = s; n++; alles.Add(s); this.Invalidate();


Download ppt "(H9.1) CirkelKlikker ARRAY int x, y; [ ] int n=0;"

Similar presentations


Ads by Google