lab int array array new intn array forint
Μαθήματα από το lab • int[][] array; array = new int[n][]; array for(int i = 0; i < n; i ++){ array[i] = new int[m]; } array null Ορισμός κενού πινάκα
class Grade. Histogram { Σωστό ή λάθος? public Grade. Histogram(int max. Grade, int[] grades) { int[] histogram = new int[max. Grade]; for (int i = 0; i < grades. length; i ++){ int x = grades[i]; histogram[x-1] ++; } } public String to. String(){ { String output = ""; for (int i = 0; i < max. Grade; i ++){ output = output + (i+1) + ": " + histogram[i] + " "; } return output; } Οι μεταβλητές max. Grade και histogram δεν είναι ορισμένες. Για να μπορεί να τις βλέπει η μέθοδος print (ή οποιαδήποτε άλλη μέθοδος) θα πρέπει να είναι ορισμένες ως πεδία της κλάσης ! Σ Ο Θ } Α Λ
class Grade. Histogram { private int max. Grade; private int[] histogram; Σωστό? public Grade. Histogram(int max. Grade, int[] grades) { int[] histogram = new int[max. Grade]; for (int i = 0; i < grades. length; i ++){ int x = grades[i]; histogram[x-1] ++; } } public String to. String(){ { String output = ""; for (int i = 0; i < max. Grade; i ++){ output = output + (i+1) + ": " + histogram[i] + " "; } return output; } } O constructor δεν αρχικοποιεί τα πεδία της κλάσης. Οι μεταβλητές max. Grade και histogram που ορίζονται μέσα στον constructor είναι τοπικές μεταβλητές και δεν αλλάζουν την τιμή των πεδίων. Α Λ Ο Θ ! Σ
class Grade. Histogram { private int max. Grade; private int[] histogram; Σωστό? public Grade. Histogram(int max. Grade, int[] grades) { this. max. Grade = max. Grade; for (int i = 0; i < grades. length; i ++){ int x = grades[i]; histogram[x-1] ++; } } public String to. String(){ { String output = ""; for (int i = 0; i < max. Grade; i ++){ output = output + (i+1) + ": " + histogram[i] + " "; } return output; } } H μεταβλητή max. Grade αρχικοποιείται σωστά. Ο πίνακας histogram όμως όχι. Τον έχουμε ορίσει σωστά αλλά δεν τον έχουμε δημιουργήσει (δεν του έχουμε δώσει χώρο)! Δεν έχουμε προσδιορίσει το μέγεθος του Α Λ Ο Θ ! Σ
class Grade. Histogram { private int max. Grade; private int[] histogram = new int[max. Grade]; public Grade. Histogram(int max. Grade, int[] grades) { this. max. Grade = max. Grade; for (int i = 0; i < grades. length; i ++){ int x = grades[i]; histogram[x-1] ++; } } Σωστό? public String to. String(){ { String output = ""; for (int i = 0; i < max. Grade; i ++){ output = output + (i+1) + ": " + histogram[i] + " "; } return output; } } Θυμηθείτε ότι οι εντολές αυτές θα εκτελεστούν πριν από τις εντολές του constructor. Εκείνη τη στιγμή δεν ξέρουμε το μέγιστο βαθμό και άρα δημιουργούμε ένα πίνακα μηδενικού μεγέθους! Α Λ Ο Θ ! Σ
class Grade. Histogram { private int max. Grade; private int[] histogram; Σωστό? public Grade. Histogram(int max. Grade, int[] grades) { histogram = new int[max. Grade]; for (int i = 0; i < grades. length; i ++){ int x = grades[i]; histogram[x-1] ++; } } public String to. String(){ { String output = ""; for (int i = 0; i < max. Grade; i ++){ output = output + (i+1) + ": " + histogram[i] + " "; } return output; } } O Constructor θα αρχικοποιήσει σωστά τον πίνακα histogram, αλλά δεν θα αλλάξει το πεδίο max. Grade μιας και χρησιμοποιεί την τοπική μεταβλητή - παράμετρο To max. Grade εδώ αναφέρεται στο πεδίο και έχει τιμή μηδέν. Α Λ Ο Θ ! Σ
class Grade. Histogram { private int max. Grade; private int[] histogram; Σωστό? Πρώτα δηλώνουμε τα πεδία μέσα στην κλάση public Grade. Histogram(int max. Grade, int[] grades) { this. max. Grade = max. Grade; histogram = new int[max. Grade]; for (int i = 0; i < grades. length; i ++){ int x = grades[i]; histogram[x-1] ++; } } public String to. String() { String output = ""; for (int i = 0; i < max. Grade; i ++){ output = output + (i+1) + ": " + histogram[i] + " "; } return output; } } Στον Constructor δίνουμε τιμή στο max. Grade και αφού πλέον ξέρουμε το μήκος του πίνακα τον δημιουργούμε και του δίνουμε χώρο για να κρατάει τις τιμές. Τώρα μπορούμε και να κάνουμε και την αρχικοποίηση του πίνακα Ω Σ ! Ο Τ Σ
Παράδειγμα class Grade. Histogram { Ορισμός private int max. Grade; μεταβλητής private int[] histogram; πίνακα public Grade. Histogram(int max. Grade, int[] grades) { this. max. Grade = max. Grade; Δημιουργία histogram = new int[max. Grade]; πίνακα for (int i = 0; i < grades. length; i ++){ int x = grades[i]; histogram[x-1] ++; } } Οι κόκκινες μεταβλητές υπάρχουν } μόνο μέσα στο μπλοκ της μεθόδου Οι μπλε μεταβλητές είναι πεδία
Κλάσεις και αντικείμενα Grade. Histogram max. Grade histogram[] Ορισμός της κλάσης Grade. Histogram(int, int[]) to. String() add. Histogram(Grade. Histogram) equals(Grade. Histogram) hist 2 = new Grade. Histogram(5, grades 2) Grade. Histogram max. Grade = 5 histogram = {1, 2, 1, 1, 1} Grade. Histogram(int, int[]) to. String() add. Histogram(Grade. Histogram) equals(Grade. Histogram) hist 3 = new Grade. Histogram(5, grades 3) Grade. Histogram max. Grade = 5 histogram = {1, 1, 2, 1, 0} Grade. Histogram(int, int[]) to. String() add. Histogram(Grade. Histogram) equals(Grade. Histogram)
Κλάσεις και αντικείμενα Grade. Histogram max. Grade histogram[] Ορισμός της κλάσης Grade. Histogram(int, int[]) to. String() add. Histogram(Grade. Histogram) equals(Grade. Histogram) hist 2. add. Histogram(hist 3); hist 2 = new Grade. Histogram(5, grades 2) Grade. Histogram max. Grade = 5 histogram = {2, 3, 3, 2, 1} Grade. Histogram(int, int[]) to. String() add. Histogram(Grade. Histogram) equals(Grade. Histogram) hist 3 = new Grade. Histogram(5, grades 3) Grade. Histogram max. Grade = 5 histogram = {1, 1, 2, 1, 0} Grade. Histogram(int, int[]) to. String() add. Histogram(Grade. Histogram) equals(Grade. Histogram)
class Grade. Histogram { private int max. Grade; private int[] histogram; private String output = “”; Σωστό? public Geometric(int max. Grade, int[] grades) { this. max. Grade = max. Grade; histogram = new int[max. Grade]; for (int i = 0; i < grades. length; i ++){ x = grades[i]; histogram[x-1] ++; } } H μεταβλητή output πλέον είναι πεδίο. Οι αλλαγές της τιμής της παραμένουν στο αντικείμενο public String to. String(){ { for (int i = 0; i < max. Grade; i ++){ output = output + (i+1) + ": " + histogram[i] + " "; } return output; } } Τι γίνεται αν κάνουμε πολλαπλές κλήσεις της μεθόδου to. String? Α Λ Ο Θ ! Σ
Contact. List copy-constructor class Contact. List Είναι αυτή η υλοποίηση σωστή? { private Contact[] contacts; Αντιγράφουμε την αναφορά private int size; στον πίνακα ενώ πρέπει να private int contact. Num = 0; δημιουργήσουμε ένα καινούριο πίνακα αντίγραφο public Contact. List(Contact. List other) { this. contacts = other. contacts; } ΛΑ ! Σ ΘΟ
Contact. List copy-constructor class Contact. List { private Contact[] contacts; private int size; private int contact. Num = 0; Είναι αυτή η υλοποίηση σωστή? H μεταβλητή size δεν έχει αρχικοποιηθεί. Όλες οι μεταβλητές πρέπει να αρχικοποιηθούν κανονικά όπως σε όποιο άλλο constructor public Contact. List(Contact. List other) { this. contacts = new Contact[size]; for (int i = 0; i < size; i++){ this. contacts[i] = other. contacts[i]; } } ΛΑ ! Σ ΘΟ
Contact. List copy-constructor class Contact. List Είναι αυτή η υλοποίηση σωστή? { private Contact[] contacts; private int size; private int contact. Num = 0; public Contact. List(Contact. List other) { this. size = other. size; this. contacts = new Contact[size]; for (int i = 0; i < size; i++){ this. contacts[i] = other. contacts[i]; } } Δημιουργούμε βαθύ αντίγραφο του πίνακα, αλλά όχι των εγγραφών (Contacts) του πίνακα. ΛΑ ! Σ ΘΟ
Contact. List copy-constructor class Contact. List { private Contact[] contacts; private int size; private int contact. Num = 0; Είναι αυτή η υλοποίηση σωστή? Έχουμε μόνο contact. Num εγγραφές στον πίνακα. Τα υπόλοιπα είναι null. Αν προσπαθήσουμε να δημιουργήσουμε αντίγραφο null αντικειμένου θα πάρουμε λάθος. public Contact. List(Contact. List other) { this. size = other. size; this. contacts = new Contact[size]; Φωλιασμένη κλήση του copy- for (int i = 0; i < size; i++){ this. contacts[i] = constructor της new Contact(other. contacts[i]); κλάσης Contact. } } ΛΑ ! Σ ΘΟ
Contact. List copy-constructor class Contact. List { private Contact[] contacts; private int size; private int contact. Num = 0; Είναι αυτή η υλοποίηση σωστή? public Contact. List(Contact. List other) { this. size = other. size; this. contact. Num = other. contact. Num; this. contacts = new Contact[size]; for (int i = 0; i < contact. Num; i++){ this. contacts[i] = new Contact(other. contacts[i]); } } Ω ! Ο Τ Σ
- Slides: 41