ניתוח מערכות מידע ב

Post on 05-Jan-2016

62 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

ניתוח מערכות מידע ב. נעים להכיר... תכנות ב C#. ניתוח מערכות מידע ב'. מרצים מכון לב - ד"ר אבי רוזנפלד מכון טל – ד"ר אריאלה ריכרדסון שעות קבלה: בתאום מראש בדוא"ל אתר הקורס – http://www.jct.ac.il/~rosenfa/nituach2 עדכונים מצגות דוגמאות וחומר לחזרה סילבוס. נושאים שילמדו בקורס. - PowerPoint PPT Presentation

TRANSCRIPT

1

ניתוח מערכות מידע ב

נעים להכיר...

#Cתכנות ב

ניתוח מערכות מידע ב'

מרציםמכון לב - ד"ר אבי רוזנפלדמכון טל – ד"ר אריאלה ריכרדסון :שעות קבלה

בתאום מראש בדוא"ל

– אתר הקורסhttp://www.jct.ac.il/~rosenfa/nituach2 עדכוניםמצגותדוגמאות וחומר לחזרהסילבוס

נושאים שילמדו בקורס

הכרות עםC#תיכנון מונחה עצמים מתקדם

Polymorphism, composition, association תרשימיUMLמתאימים

בניית ממשק למשתמש הכרותADO

שילוב בין בסיסי נתונים וC#פרויקט מסכם

ספרים

Requirements Analysis and System Design, 3rd ed. Leszek A. Maciaszek

Object-Oriented Systems Analysis and Design Using UML, 3rd ed. , Simon Bennett, Steve Mcrobb, Ray Farmer

#C  כשפת אם. – Object Oriented Design .net framework. יצחק גרבר.

Visual C# 2010, How to Program (4th Edition), Deitel and Deitel.

Pro Asp.Net 4 in C# 2010, 4th ed. Matthew MacDonald, Adam Freeman, and Mario Szpuszta.

5

#Cהכרות עם

פקודות כלליות

6

ופלט דוגמא - טיפול בקלט

using System;

class IOProgram{

static void Main(string[] args){

Console.WriteLine("Please Type Something:");

String x;x = Console.ReadLine();Console.WriteLine("You typed {0}" , x );Console. ReadKey();

}}

7

קלט של מספרים

using System;

class numberProgram{

static void Main(string[] args){

String x;int y;Console.WriteLine("Enter a number:");x = Console.ReadLine();y = int.Parse(x);Console.WriteLine("You typed {0}" ,x);Console.WriteLine("Adding 2 makes: {0} or {1}", x+2, y+2 );

Console. ReadKey();}

}

8

ifדוגמא -

using System;

class ifProgram{

static void Main(string[] args){

string x;x = Console.ReadLine();if (x == "a" || x == "A")

Console.WriteLine("A is for apple");else if (x == "b" || x == "B")

Console.WriteLine("B is for Boy");else

Console.WriteLine("Not defined");

Console.ReadKey();}

}

9

forדוגמא -

using System;

class ifProgram{

static void Main(string[] args){

for (int i = 0; i < 100; i++) Console.WriteLine("The square root of {0} is {1} ", i,

Math.Sqrt(i) );

Console.ReadKey();}

}

10

whileדוגמא - using System;class FirstProgram{

static void Main(string[] args){

int NUM_GAMES = 12;int won;double ratio;

Console.WriteLine("Enter num of games won (0 to {0})", NUM_GAMES);string tmp = Console.ReadLine();won = int.Parse(tmp);

while (won < 0 || won > NUM_GAMES){

Console.WriteLine ("Invalid input. Please reenter: ");tmp = Console.ReadLine();won = int.Parse(tmp);

}ratio = (double)won / NUM_GAMES;

Console.WriteLine ("Winning percentage is: {0}", ratio);}

}

11

int grade, category;

Console.WriteLine("Enter a numeric grade (0 to 100): ");string tmp = Console.ReadLine();grade = int.Parse(tmp);

category = grade / 10;

switch (category){

case 10:Console.WriteLine("Perfect score. Well done.");break;

case 9:Console.WriteLine("Well above average. Excellent.");break;

case 8:Console.WriteLine("Above average. Nice job.");

break;case 7:

Console.WriteLine("Average.");break;

default:Console.WriteLine("Study some more.");break;

}

12

ל VBהבדלים בין אופרטורים של C#

Purpose VB.NET C#

Integer division \ /

Modulus (division returning only the remainder)

Mod %

Exponentiation ^ n/a

Integer division Assignment \= /=

Concatenate &= NEW +=

Modulus n/a %=

Bitwise-AND n/a &=

Bitwise-exclusive-OR n/a ^=

Bitwise-inclusive-OR n/a |=

Equal = ==

Not equal <> !=

Compare two object reference variables Is ==

Compare object reference type TypeOf x Is Class1 x is Class1

Concatenate strings & +

Shortcircuited Boolean AND AndAlso &&

Shortcircuited Boolean OR OrElse ||

Scope resolution . . and base

13

#C ל VBהבדלים בין אופרטורים של המשך...

Purpose VB.NET C#

Array element () [ ]

Type cast Cint, CDbl, ..., CType )type)

Postfix increment n/a ++

Postfix decrement n/a --

Indirection n/a * (unsafe mode only)

Address of AddressOf & (unsafe mode only; also see fixed)

Logical-NOT Not !

One's complement Not ~

Prefix increment n/a ++

Prefix decrement n/a --

Size of type n/a sizeof

Bitwise-AND And &

Bitwise-exclusive-OR Xor ^

Bitwise-inclusive-OR Or |

Logical-AND And &&

Logical-OR Or ||

Conditional If Function () ?:

Pointer to member n/a . )Unsafe mode only)

14

מחלקות

15

עקרונות בתכנון מונחה עצמים

תכנון מונחה עצמים תומך בעקרונות הבאים

– הכמסהencapsulation

– ירושהinheritance

פולימורפיזם - polymorphism

נלמד עכשיו

נלמד בהמשך

16

חשבון הבנק – מחלקה ואוביקט

מחלקההרעיון/)(תבנית

אוביקטים(המימוש)

תכונות

פעולות

שם מחלקה

שם מחלקה

שם אוביקט

תכונות +

ערכים

17

נראה תחילה איך מייצרים מחלקה

18

)encapsulationמהי הכמסה (

אפשר להסתכל על מחלקה בשני אופנים

פנימי – הפרטים של המחלקה, התכונות והפעולות שלה

חיצוני – השירותים שהמחלקה מספקת

חיצוניפנימי

19

)encapsulationמהי הכמסה (

אפשר להסתכל על מחלקה בשני אופנים

פנימי – הפרטים של המחלקה, התכונות והפעולות שלה

חיצוני – השירותים שהמחלקה מספקת

חיצוניפנימי

20

public מאפשר גישה גם מחוץ - למחלקה

תכונות

יוגדרו לאבדרך כלל תכונות public

מתודות

בדרך כלל יוגדרוpublic שכן הן מיועדות לשימוש מחוץ למחלקה

מאפשר גישה למתודות מחוץ למחלקה

מאפשר גישה מכל מתודות המחלקה

21

get, set

כיוון שאין גישה לתכונות המחלקה באופן ישיר, מקובללהגדיר מתודות גישה לתכונות

מתודות אלו נקראות בשמות

getX מחזירה את ערך התכונה - X נקרא גם) accessor(

setX קובע את ערך התכונה - X נקרא גם) mutator(

מתודותget -ו set מאפשרות גישה מבוקרת לתכונות המחלקה

ניתן להגביל טווח ערכים

ניתן לבדוק שהערכים תקינים

כאשר אין צורך לא נגדיר לתכונות מתודות get וset

VOIDדוגמא של פונקציה של

22

namespace ConsoleApplication1{ class Program { public static void XYZ() { Console.WriteLine("There"); } public static void Do(int x) { Console.WriteLine(x+2); } static void Main(string[] args) { XYZ(); Do(4); int y = 3; Do(y); Console.WriteLine("Hi"); } }}

דוגמא של פונקציה שמחזירה ערך

23

namespace ConsoleApplication1{ class Program { public static int XYZ(int x) { return x + 2; } static void Main(string[] args) { XYZ(4); int y = 3; XYZ(y); Console.WriteLine("Hi"); } }}

דוגמא של מערך

24

namespace ConsoleApplication1{ class Program { static void Main(string[] args) { int[] numbers = new int[10]; // numbers is a 10-element array numbers[0] = 1; numbers[2] = -3; for (int i = 0; i < 10; i++) Console.WriteLine("pos " + i + " is " + numbers[i]); } }}

25

מחלקה למימוש #Cקוד BankAccount

using System;public class BankAccount{

private String name;private float balance;

public BankAccount(String name) { //constructor body }

public float getBalance( ) { //method body }

public void deposit(float sum) { //method body }

public void draw(float sum) { //method body }}

תכונות

פעולה )מתודה(

בנאי (constructor )

26

1המימוש המלא – חלק

using System;

public class BankAccount{

//attributesprivate String _name;private float _balance;

//constructorpublic BankAccount(String name){

_name = name;_balance = 0;

}

המשך בשקף הבא

27

2המימוש המלא – חלק

//methodspublic float getBalance( ){

return _balance;}

public void deposit(float sum){

_balance += sum;}

public void draw(float sum){

_balance -= sum;}

}

28

מתודות - דגשים

דגשים

29

כותרת המתודה

הצהרת מתודה מתחילה בכותרת

public int calc (int num1, int num2)

שם המתודה

טיפוס הערך המוחזר

רשימת פרמטרים

רשימת הפרמטרים מגדירה עבור כל פרמטר את הטיפוס שלו ואת שמו

סוג גישה

30

גוף המתודה

לאחר הכותרת מגיע גוף המתודה

public int calc (int num1, int num2)

{ int sum = num1 + num2;

return sum;}

הטיפוס שמחזירים צריך להיות זההלטיפוס הערך המוחזר

sumהוא משתנה לוקלי

הוא מיוצר בכל פעם שהמתודה נקראת ומושמד

כאשר מסיימת להתבצע

31

returnפקודת

של טיפוסטיפוס הערך המוחזר של מתודה קובע את ה הערך שיוחזר למקום ממנו נקראה המתודה

למתודה שלא מחזירה ערך יש טיפוס ערך מוחזרvoid

public int calc (int num1, int num2)

public void calc (int num1, int num2)

{ int X; // do something return X;}

{ // do something}

32

(המשך)returnפקודת

-פקודת הreturn המוחזרערךה קובעת את

הערך שיש בXיוחזר

ניתן גם להחזיר ערך של ביטוי

הערך המוחזר ע"י הביטוי צריך להתאים לטיפוס הערךהמוחזר

public int calc (int num1, int num2){ int X; // do something return X;}

public bool calc (int num1, int num2){ return num1<num2;}

33

contructorבנאי -

) בנאיconstructorהיא מתודה מיוחדת (לכל מחלקה יש להגדיר בנאי

שם הבנאי זהה לשם המחלקההבנאי אינו מחזיר ערך

הבנאי נקרא באופן אוטומטי כאשר אוביקט נוצרבדרך כלל נבצע פעולות של איתחול בבנאייתכנו מספר בנאים למחלקה אחת

לכל אחד מהבנאים יש לקבוע רשימת פרמטריםשונה

34

בנאים

ללא בנאי יכול להופיעעם פרמטריםאו פרמטרים

,אפשר להגדיר מספר בנאיםאם יש לכל אחד פרמטרים

שונים

מספר שונה שלפרמטירם

או

טיפוסים שונים שלפרמטרים

public class BankAccount{

private String _name;private float _balance;

//constructor without parameterspublic BankAccount(){

_name = null;_balance = 0;

}

//constructor with parameterspublic BankAccount(String name){

_name = name;_balance = 0;

}{

35

בנאים - ערך מוחזר

ערך מוחזר!!! אפילו לא איןלבנאיםvoid

טעות נפוצה – לתת טיפוס לבנאי

גורם לבנאי להיות מתודה רגילה

:במידה ולא הוגדר בנאי למחלקה

יוגדר באופן אוטומטי בנאי ריק

לבנאי הריק אין פרמטרים

הבנאי הריק לא מבצע שום פעולה

36

שימוש במחלקות ואוביקטים

דגשים

37

יצרנו את המחלקה,עכשיו נרצה לייצר אוביקטים

אוביקטים(המימוש)

38

של אוביקטים ליצירת #Cקוד BankAccount

using System; public class test {

static void Main(String[] args) { BankAccount Account1 = new BankAccount("Tamar"); BankAccount Account2 = new BankAccount("Moshe");

Account1.deposit(20000); Account2.deposit(100); Account2.draw(600);

}

}

יצירת אוביקטי

םשימוש בפעולות

39

יצירת אוביקטים

) משתנה מכיל טיפוס בסיסיint, float… (הצבעה לאוביקטאו

ניתן להצהיר על משתנה מטיפוס מחלקה כלשהיCircle circ1;

נוצר אוביקט בהצהרה הנ"ל!!לא

המשתנה מכיל כתובת בזכרון עבור האוביקט

יש לייצר את האוביקט במפורש ע"י שימוש בnew המבצע קריאה לבנאי

circ1 = new Circle ();

Circle circ1 = new Circle (); ניתן לשלב את שתי הפעולות ביחד:

diameter = 30;xPosition = 20;yPosition = 60;color = "blue;"

isVisible = false;

circ1

40

הפעלת מתודות

' כדי להפעיל .ברגע שיצרנו אוביקט אפשר להשתמש באופרטור 'מתודות שלו.

int size=0;

Circle circ1 ;

circ1 = new Circle;()

circ1.moveHorizontal(10);

size = circ1.getSize;()

מתודה יכולה להחזיר ערך (כמוgetSize() (

ניתן להשתמש בערך לביצוע השמה או בתוך ביטוי

"אפשר לחשוב על קריאה למתודה כ"הפעלת שירות של המחלקה

41

ראינו כבר שאפשר לייצר כמה בנאים

ניתן לתת הגדרות שונות גם למתודות רגילות

) העמסהoverloadingיש מספר מתודות עם אותו שם – (

) על החותמתsignature ,של המתודה להיות יחודית (כלומר:

מספר הפרמטרים, הטיפוס שלהם והסדר שלהם

חלק מהחותמתאינוהטיפוס המוחזר

כלומר אסור ששני מתודות יבדלו רק ע"י הטיפוס המוחזר

overloadingהעמסה -

42

העמסה – רשימת הפרמטרים

הקומפיילר קובע לאיזה מתודה לקרוא ע"י ניתוח הפרמטרים

static float tryMe(int x){ return x + 10;}

static float tryMe(int x, int y){

return x*y;}

result = tryMe(25, 5)

?מי יופעל

43

null אוביקטים הוא ערך מיוחד עבור משמעותnull"הוא "שום דבר ) טיפוסים פרימיטיביםint, float… , לא יכולים לקבל (null מקביל לNothing ב VB

ניתן לתת לאובייקט ערךnull:בצורה הבאה String s = null;

אפשר גם לבדוק אם אובייקט מסויים הואnull:if (s == null) // Do something

האובייקטים שהוכרזו אך לא אותחלו - מכילים ערךnull. ניסיון לבצע פעולה על אובייקט שהואnull יגרור קריסה של

התוכנית

null

44

Enumerated Types

משתמשים בenum כדי להגדיר טיפוס המקבל ערכים מוגדריםמראש

שקול לEnum של VB

למשל נגדיר טיפוס שנקרא Season

enum Season {winter, spring, summer, fall};

ניתן להגדיר כמה ערכים שנרצה

לאחר ההגדרה ניתן להגדיר משתנים מהטיפוס החדש

Season time;

ולאתחל ערכים

time = Season.fall;

45

בנאי העתקה

46

Pointהמחלקה

נגדיר מחלקה בשםPoint.של נקודה במרחב

למחלקה שני מאפיינים, רכיבX ורכיב Y

public class Point

{

private double _x,_y;

public Point(double x, double y) { _x = x; _y = y;}

public double getX() { return _x; }

public double getY() { return _y; }

public void setX(double x) { _x = x; }

public void setY(double y) { _y = y; }

}

47

toStringהוספת מתודה

public class Point

{

private double _x,_y;

public Point(double x, double y) { _x = x; _y = y;}

public double getX() { return _x; }

public double getY() { return _y; }

public void setX(double x) { _x = x; }

public void setY(double y) { _y = y; }

public String toString()

{//return the relevant string

return _x + "," + _y;

}

}

48

השמה של אוביקטים

עבור אוביקטים השמה מעתיקה את הכתובת

Point p2 = p1;

p1Before: 1 2

p1

p2Then:

1 2

p2.setX(5);

p1

p2After:

5 2

49

מה שרוצים:

שלו זהה לישןהתוכןלייצר אוביקט חדש ש

Point p2 = new Point(p1)

p1Before: 1 2

p2.setX(5);

p2

p1Then:

1 2

1 2

p1After:

1 2

5 2p2

50

הפתרון: בנאי העתקה

בנאי העתקהcopy constructor

משכפל אוביקט

מייצר אוביקט חדש

מאתחל את האוביקט החדש באופן זהה לישןp1 1 2p2 1 2

51

copyהוספת בנאי העתקה (constructor(

public class Point{ private double _x,_y; public Point(double x, double y) { _x = x; _y = y;} //copy constructor public Point(Point otherPoint) { _x = otherPoint._x; _y = otherPoint._y; }

public double getX() { return _x; } public double getY() { return _y; } public void setX(double x) { _x = x; } public void setY(double y) { _y = y; } public String toString() {//return the relevant string return _x + "," + _y; }}

52

העברת אוביקטים

53

העברת אוביקט כפרמטר

אפשר להעביר אוביקט כפרמטר - נראה דוגמא במחלקהPoint

נממש מתודה שמחשבת מרחק בין הנקודה של האוביקט לנקודהאחרת

2 נקודות מוגדר:2מרחק בין12

212 )()( yyxxdist

public double distance(Point otherPoint)

{

double diffX = Math.Pow(_x-otherPoint._x,2);

double diffY = Math.Pow(_y-otherPoint._y,2);

return Math.Sqrt(diffX+diffY);

}

54

Pointהחזרת אוביקט ממתודה ב

נממש מתודה שמחזירה את הנקודה באמצע הדרך) לנקודה של האוביקט0,0מהמרכז (

) 5,2) תחזיר (10,4עבור הנקודה(

) 2,3.5) תחזיר (4,7עבור הנקודה(

public Point halfPoint()

{

double halfX = _x/2;

double halfY = _y/2;

Point halfPoint = new Point)halfX, halfY(;

return halfPoint;

}

55

עוד דרך להחזרת אוביקט:

public Point halfPoint()

{

return new Point)_x/2,_y/2(;

}

אפשר לבצע את הnew בתוך פקודת ה return

56

halfPointדוגמא לשימוש במתודה

Point p1 = new Point(10,4);

Point p2 = new Point(4,7);

Point middlePoint = p1.halfPoint();

Console.WriteLine(p2.halfPoint());

פלט:2.0,3.5

57

אוביקט כתכונה

Aggregation

58

.נרצה להגדיר למחלקה תכונות שהן מחלקה אחרת שיצרנו

מאפשר שימוש חוזר של קוד ובנייה של מחלקות מורכבות

נניח נרצה ליצור מחלקה המתארת מעגל

הגדרנו מחלקה עבור נקודה

Point

נגדיר מחלקה עבור מעגל

המרכז (מסוג 1תכונה :Point(

רדיוס2תכונה :

radius

אוביקט כתכונה

Point

59

הגדרת המעגלpublic class Circle { private double _radius; private Point _center;

//constructor 1 public Circle(double r, double x, double y) { _radius = r; _center = new Point(x, y); }

//constructor 2 public Circle(double r, Point p) { _radius = r; _center = new Point(p); } }

שימו לב!בכל הבנאים

על מנת לאתחל את המרכזnew יש לבצע

60

הגדרת המעגלpublic class TestCircle{ static void Main(String[] args) {

//making a circle using radius and x,y coordiantes Circle circ1 = new Circle(10,2,4);

//making a circle using a radius, and a point Point pt = new Point(5,6); Circle circ2 = new Circle(20, pt);

what if we want to print the circle information?

} }

61

הגדרת המעגלpublic class Circle { private double _radius; private Point _center;

//constructor 1 public Circle(double r, double x, double y) { _radius = r; _center = new Point(x, y); }

//constructor 2 public Circle(double r, Point p) { _radius = r; _center = new Point(p); }

//toStringpublic String toString()

{ return ("radius is " + _radius + ",\t" + "center is " + _center); }}

כאן נקראת מתודת toString

של Point

62

הגדרת המעגלpublic class TestCircle{

static void Main(String[] args) {

//making a circle using radius and x,y coordiantes Circle circ1 = new Circle(10,2,4);

//making a circle using a radius, and a point Point pt = new Point(5,6); Circle circ2 = new Circle(20, pt);

//Now we can print Console.WriteLine("circ1: " + circ1.ToString());

Console.WriteLine("circ2: " + circ2.ToString());}

}

63

הגדרת המעגלpublic class Circle { private double _radius; private Point _center;

//constructor 1 public Circle(double r, double x, double y) { _radius = r; _center = new Point(x, y); }

//constructor 2 public Circle(double r, Point p) { _radius = r; _center = new Point(p); }

//toString public String ToString() { return ("radius is " + _radius + ",\t" + "center is " + _center.ToString()); }

public void setRadius(double r) { _radius = r; }

public double getRadius() { return _radius;

} }

set & get

)doubleעבור רדיוס (טיפוס

64

המתודה תקבל את הקואורדינטותx ו y

setCenter 'אפשרות א -

public void setCenter(double x, double y) { _center.setX(x); _center.setY(y); }

public void setCenter(double x, double y) { _center._x=x; _center._y=y; }

לא ניתן לגשת לתכונות centerפרטיות של

!!!Pointמסוג

65

public void setCenter(Point p) { _center._x = p._x; _center._y = p._y; }

(נקודה) המתודה תקבל מרכז

(העברת אוביקט כפרמטר)

setCenter 'אפשרות ב -

public void setCenter(Point p) { _center.setX(p.getX()); _center.setY(p.getY()); }

public void setCenter(Point p) { _center = new Point(p); }

או

משתמש בבנאי העתקה

Point של

public void setCenter(Point p) { _center =p; }

aliasing private

66

:הדרך הנכונה להחזרת המרכז

getCenter הנכון -

public Point getCenter() { return new Point(_center); }

במקרה זה יוחזר העתק של התכונה

שימוש בבנאי העתקה

) עקרון ההכמסהencapsulation.נשמר (

אם היינו מחזירים בליnewהיה אפשר לשנות מחוץ למחלקה

67

הגדרת המעגלpublic class TestCircle{ static void Main(String[] args) {

Circle circ1 = new Circle(10, 2, 4);

//set and get as usual circ1.setRadius(100); double rad2 = circ1.getRadius();

//set using coordinates circ1.setCenter(7, 8);

//set using a Point Point p3 = new Point(9, 9); circ1.setCenter(p3);

//get returning a Point Point center2 = circ1.getCenter();

} }

דוגמאות לשימושset, get:

68

מתודה המחזירה שטח המעגל

Circleנרחיב את המחלקה

public double area() { return _radius * _radius * Math.PI; }

public double perimeter() { return _radius * 2 * Math.PI; }

מתודה המחזירה היקף המעגל

69

נרחיב את המחלקהCircle

מתודה הבודקת אם המעגל מוכל כולו ברביע הראשון

?איך נדע

מעבדה

public boolean isInFirstQuarter() { return (_center.getX() - _radius >=0 && _center.getY() - _radius >=0); }

70

נרחיב את המחלקהCircle

מתודה הבודקת אם נקודה היא בתוך המעגל

?מה צריך לבדוק

2מעבדה

public boolean isPointInside(Point p) {

return (_center.distance(p) <_radius ); }

71

C#מערכים

72

:ניתן להצהיר על מערך באופן הבא

int[] scores;

הצהרה כנ"ל מגדירה רק קיום של מערך ללא המערך עצמו

scores מצביע כעת לכתובת null

) אם רוצים לייצר מופעinstance של המערך נשתמש ב (new:

scores = new int[10];

10זה יגדיר מערך של int

מערך – הצהרה ויצירה

73

int[] prices = new int[500];

דוגמאות להצהרה ויצירה

bool[] flags;

int length = 20;

flags = new bool[length];

char[] codes = new char[1750];

מערך שגודלו נקבע בזמן ריצה:

int n = int.Parse(Console.ReadLine());

int[] a = new int[n];

74

מערכים – גישה לתא בודד0 1 2 3 4 5 6 7 8 9

79 87 94 82 67 98 87 81 74 91

(סוגריים מרובעים) [ ] מתיחסים לתא בודד במערך ע"י למשלscores[3] 82 מתיחס לתא הרביעי במערך, וערכו. התא הראשון הואscores[0] המערך הזה הוא שלint ולכן בתא בודד ניתן להשתמש כמו כל

intאחר

int a = scores[2];

mean = (scores[0] + scores[1])/2;

Console.WriteLine ("Top = " + scores[5]);

scores

75

Lengthמייצג את מספר האיברים במערך

int a = scores.Length;

לa 10 יהיה הערך

Console.WriteLine(scores.Length);

10ידפיס

:שימו לב

Length האינדקס הכי גבוה!ולא מכיל את מספר האיברים

האינדקס הכי גבוה הואLength-1

אורך מערך

76

ניתן ליצור מערך ולמלא בו ערכים ע"י שימושברשימת אתחול

רשימת אתחול

int[] units = {147, 323, 89, 933, 540, 269, 97, 114}

char[] letterGrades = {'A', 'B', 'C', 'D', 'F'};

הערכים מופרדים ע"י פסיק

גודל המערך נקבע לפי אורך הרשימה

אין צורך להשתמש בnew

אפשר להשתמש ברשימת אתחול רק בזמן הגדרת המערך

77

מערך - הדפסת כל האיברים

int[] arr = {1,2,3,4,5};

for(int i =0; i<arr.Length; i++){

Console.WriteLine(arr[i]);}

(נעבור על כל התאים שלו) יש לעבור על המערך

אורכוarr.Length

נדפיס את התוכן של התא

top related