SlideServe
Menu
Préférences
© SlideServe · v2026

Mastering Arrays & Strings in Java & Ada: Scope, Exceptions, Dynamic Arrays, & More

fallon-bowers avatar
22 diapositives · 312 Views · Nov 09, 2014

Learn about scopes, exceptions, arrays, dynamic arrays, & more in Java & Ada. Understand values passing, comparisons, slices, strings, loops, & differences between languages. Practice with examples & grasp the nuances of array manipulation.

Mastering Arrays & Strings in Java & Ada: Scope, Exceptions, Dynamic Arrays, & More - slide 1
01 / 22Mastering Arrays & Strings in Java & Ada: Scope, Exceptions, Dynamic Arrays, & More
1 / 17

Télécharger la présentation

Mastering Arrays & Strings in Java & Ada: Scope, Exceptions, Dynamic Arrays, & More
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. ITEC 320 Lecture 6 More on arrays / Strings

  2. Review • Scope • Exceptions • Arrays • Dynamic arrays • How? • Typed and anonymous

  3. Issue • Named type required for passing to functions / procedures What is values 1? Can’t duplicate when passing to a function. procedure demo is values1: array (1 .. 50) of integer; values2: array (1 .. 50) of integer; begin values1 := (2 => 4, 4 => 16, 5 => 25, 3 => 9, others => 0); values2 := (others => 0); end demo;

  4. Comparisons type Myarray is array (1 .. 3) of Integer; a, b: Myarray; ... a := (10, 20, 30); b := (others => 0); if a = b then ... b := a; if a = b then ... Note: types must be the same Checks each element! Assignment works element by element Is this shallow or deep copying?

  5. Java • Behavior • Java references • Ada values int[] a = {10, 20, 30}; int[] b = {0, 0, 0}; if (a == b) ... a = b; if (a == b) ...

  6. Attributes • 'first • 'last • 'range • 'length • Why is it important to know these?

  7. Slices • Portions of the whole • What are the advantages / disadvantages? • How would you do this in Java? s: String := "Hi Mom!"); put(s(3 .. 5) & "?"); s(4) := 'T'; put(s); s(4 .. 6) := "Bob"; put(s); s(4 .. 4) := "R"; put(s);

  8. Considerations • Sending a slice to a function / procedure • Can it be a constrained array? • What happens when you assign slices to each other? • Dynamic slice bounds (not hard coded)

  9. Example declare s: String := "Hi Mom!"; begin put_line(s(4..7) & s(1..2)); and declare s: String := "Hi Mom!"; begin put_line(s(4..7) & s(3..3) & s(1..2)); put_line(s(4..7) & s(3) & s(1..2));

  10. String’s • What do you remember about them? • Declaration? • Input? • Output?

  11. Code • What is the difference between s: String := “Hello World”; And s2: String(1..15);

  12. Issues: types must match sizes have to match Modification s: String := "Hi World"; begin put(s’first); put(s’last); s(4) := 'T'; put_line(s); s(4 .. 6) := "Ron"; put_line(s); s := ”Bye world"; put_line(s);

  13. Modification(2) • Combinations s: String := "HiMom!"; u: String(1 .. 10); ... -- s := u; -- Compileerror -- u := s; -- Compileerror s := "HiBob!"; -- Okay -- s := "HiBilly-Bob!"; -- Compileerror -- s := s & "!"; -- Compileerror u = s & "!!!";

  14. Loops • Why access character by character? for i in 1 .. s'length loop for i in s'first .. s'last loop for i in s'range loop

  15. Input • Common usage s: String(1..10); len: Natural begin while not end_of_file loop get_line(s, len); put_line(s(1..len)); end loop;

  16. Java • Reference versus value • Why do you think ADA is focused on values? String s; String t = "Mama!"; System.out.println(s); s = "today"; s = t;

  17. Declare • Want to resize a String? • Use same syntax as arrays get(size); declare s3: String(1..size); begin get_line(s3, size);

  18. Differences • What is the difference between put(v(1 .. len)); put(v);

  19. Power tools • Unbounded strings (Like java) with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Strings.Unbounded_Text_IO; use Ada.Strings.Unbounded_Text_IO; procedure showUnboundedis s: Unbounded_String := "first"; begin s := "different"; s := s & "!!"; put_line(s); -- different!! end showUnbounded

  20. Issues • Stack versus heap • Assignment with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Strings.Unbounded_Text_IO; use Ada.Strings.Unbounded_Text_IO; ... s: Unbounded_String := "first"; t: Unbounded_String := s; ... s := s & "!!"; put_line(s); -- first!! put_line(t) -- first;

  21. Strength/Weakness • Fixed length • Unconstrained • Unbounded type string is array (Positive range) <> of Character

  22. Summary • Arrays • Dynamic • Copying