+ All Categories
Home > Documents > 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin,...

10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin,...

Date post: 27-Oct-2019
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
14
K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 10. Felder (Arrays) Java-Beispiele: Echo.java Primzahlen.java Monate.java Version: 18. Nov. 2015 Teil 2
Transcript
Page 1: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

10. Felder (Arrays)

Java-Beispiele:

Echo.java Primzahlen.java Monate.java

Version: 18. Nov. 2015

Teil 2

Page 2: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

2 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Techniken mit Arrays:

• Boolean-Anzeige-Feld • Konstante Arrays • Parallele Arrays

Page 3: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

3 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Boolean-Arrays als "Anzeige"-Feld

Beispiel: Ermittle Primzahlen bis zu einer Grenze n

int[] prim;

3 17 2 7 5 13 11 0 1 2 3 4 5 6

Spontaner Ansatz: „sammeln“ von Primzahlen in einem Array

Page 4: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

4 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Boolean-Arrays als "Anzeige"-Feld

Beispiel:

Ermittle Primzahlen bis zu einer Grenze. Technik: Sieb des Eratosthenes Grundidee:

Streiche alle Vielfachen von bereits als Primzahl erkannten Zahlen.

boolean[] sieb; // Position i entspricht Zahl i // sieb[i] = true <-> i ist Primzahl

false true false true true true true 0 1 2 3 4 5 (Index) 6

Anfangsbelegung

false false false true true true false Endbelegung

Page 5: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

5 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Primzahl–Programm boolean[] sieb; int i, j, n; System.out.print("Primzahlgrenze: "); n = Keyboard.readInt(); // jetzt erst: Speicherplatz anfordern sieb = new boolean[n]; // alle sind potentiell Primzahl: for (i = 2; i < n; i++) sieb[i] = true; for (i = 2; i < n; i++) if (sieb[i]) { // falls i Primzahl, // streiche alle Vielfachen von i for (j = i + i; j < n; j += i) sieb[j] = false; }

• Aufgabe: abschließende Ausgabe der ermittelten Primzahlen

• sieb[0], sieb[1]?

Deklaration

Erzeugung

Page 6: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

6 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Konstante Arrays , parallele Arrays

Beispiel:

Verwaltung von Daten zu Monaten: - Namen - Anzahl der Tage

public final static String[] MONTH_NAME = { "", "Januar", "Februar", "Maerz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober","November", "Dezember"}; public final static int[] DAYS_OF_MONTH = { 0, 31, 28, 31, 30, 31, 30, 31, 30, 30, 31, 30, 31};

Page 7: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

7 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Array-Index: ermittelt zusammengehöriges Datenpaar

public final static String[] MONTH_NAME = { "", "Januar", "Februar", "Maerz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober","November", "Dezember"}; public final static int[] DAYS_OF_MONTH = { 0, 31, 28, 31, 30, 31, 30, 31, 30, 30, 31, 30, 31}; final static int MAI = 5;

"Februar" ... "Januar" "April" "Maerz" "Juni" "Mai"

1 2 3 4 5 6 7

28 ... 31 30 31 30 31

MONTH_NAME:

DAYS_OF_MONTH:

13 Monate

""

0

0

12

13 Monate, damit der Mai der 5. Monat sein kann

Page 8: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

8 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Array-Index: ermittelt zusammengehöriges Datenpaar

final static int MAI = 5;

public static void main (String[] args) {

System.out.println(

"Monat " + MONTH_NAME[MAI]

+ " hat " +

DAYS_OF_MONTH[MAI] + " Tage");

}

Ausgabe: Monat Mai hat 31 Tage

derselbe Index

Page 9: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

9 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Zweidimensionale Arrays

Mehrdimensionale Arrays

Page 10: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

10 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Zweidimensionale Arrays: Deklaration

static final int NUM_DAYS = 7; NUM_PERS = 3; static double[] [] table = new double [NUM_PERS] [NUM_DAYS];

0 1 2 3 4 5 6 0 1 2

3 Zeilen 7 Spalten

Tabelle: Jede Person eine Zeile (mit 7 personenbezogenen Daten)

Page 11: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

11 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Zugriff auf Elemente: doppelte Indizes

static double[] [] table = new double [NUM_PERS] [NUM_DAYS];

0 1 2 3 4 5 6

0

1

2

table [2] [1] table [1] [2]

Nr. der Zeile Nr. der Spalte

Page 12: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

12 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Parameterübergabe von Arrays

Page 13: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

13 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2013/14

Parameterübergabe: Java

fakultaet(x); System.out.println("Grad C");

- Grundsätzlich: nur Werteparameter (insb. alle elementaren Typen, String) - Objekte (insb. Arrays): Referenzparameter

Also: Art der Parameterübergabe in Java:

- abhängig vom Typ des Parameters - keine Schlüsselwörter zur Unterscheidung (z. B. VAR)

Page 14: 10. Felder (Arrays) - Institut für Informatik · K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16 3 . Boolean-Arrays als "Anzeige"-Feld . Beispiel: Ermittle Primzahlen

14 K. Bothe, Institut für Informatik, HU Berlin, GdP, WS 2015/16

Aktuelle Parameter vom Typ Array können anderen Wert bekommen

Methode: Summe der Array-Elemente für beliebig lange Arrays, Array dabei auf 0 gesetzt

Anwendung:

int summe_delete (int[] vektor, int sum) { int gesamt = 0; for (int i = 0; i < vektor.length; i++) { gesamt += vektor[i];

vektor [i] = 0; // vektor auf 0 setzen } sum = 0; // Parameter auf 0 gesetzt return gesamt; }

int [] v1 = new int[8]; int s = 100 . . . // v1: fuellen s1 = summe_delete(v1, s);

v1 auf 0 gesetzt:

0 0 0 0 0 0 0 0

s = 100 ungeändert

Aktuelle Parameter:


Recommended