arrays cont d arrays cont d
play

Arrays (contd) Arrays (contd) The previous example creates - PDF document

Arrays An array is a set of variables of the same type accessed by their index Arrays (Savitch, Chapter 7.1-7.2) int[] day = new int[4]; TOPICS 31 28 31 30 Array


  1. Arrays ¡ • An ¡array ¡is ¡a ¡set ¡of ¡variables ¡ of ¡the ¡same ¡type ¡ accessed ¡by ¡their ¡index ¡ Arrays (Savitch, Chapter 7.1-7.2) int[] day = new int[4]; TOPICS 31 28 31 30 • Array Basics • Array Loops • Array Programming day[0] day[2] day[1] day[3] CS 160 – Spring Semester 2015 2 Arrays ¡(cont’d) ¡ Arrays ¡(cont’d) ¡ • The ¡previous ¡example ¡creates ¡4 ¡integers. ¡ • Arrays ¡are ¡declared ¡using ¡square ¡brackets: ¡ – They ¡are ¡just ¡accessed ¡by ¡their ¡posi?on ¡ – type ¡[] ¡name; ¡ or ¡ • day[0], ¡day[1], ¡day[2], ¡day[3] ¡ – type ¡name[]; ¡ – Each ¡integer ¡has ¡its ¡own ¡value ¡ • using ¡the ¡new ¡keyword ¡ – What ¡happens ¡with ¡day[-­‑1], ¡day[4], ¡day[1000]? ¡ – type[] ¡name ¡= ¡new ¡type[size]; ¡ • Arrays ¡can ¡be ¡of ¡any ¡type ¡ – The ¡new ¡command ¡allocates ¡a ¡block ¡of ¡memory ¡ – int, ¡double, ¡char, ¡boolean, ¡String, ¡class ¡ • The ¡length ¡field ¡(instance ¡variable) ¡of ¡an ¡array ¡ – Every ¡element ¡of ¡an ¡array ¡has ¡the ¡same ¡type ¡ tells ¡you ¡how ¡many ¡elements ¡it ¡has ¡ – day.length ¡== ¡4. ¡ CS 160 – Spring Semester 2015 3 CS 160 – Spring Semester 2015 4 1

  2. Loops ¡+ ¡arrays: ¡challenge ¡ Step ¡1: ¡Decomposi?on ¡ problem ¡ • Task: ¡read ¡words ¡from ¡input ¡un?l ¡the ¡word ¡ What ¡has ¡to ¡be ¡done? ¡ • ‘quit’ ¡appears. ¡Then ¡print ¡out ¡how ¡many ¡?mes ¡ Ini?alize ¡counters! ¡– ¡another ¡loop ¡ ¡ • each ¡lowercase ¡leWer ¡appeared. ¡ – Read ¡strings ¡from ¡terminal ¡ Nested For ¡each ¡string, ¡ ¡ – loops • Ques?on: ¡where ¡do ¡you ¡start? ¡How ¡do ¡you ¡ For ¡each ¡character, ¡ • approach ¡this ¡problem? ¡ – Increment ¡counter ¡for ¡that ¡character ¡ ¡ Another Print ¡out ¡counts ¡per ¡character ¡ – loop CS 160 – Spring Semester 2015 5 CS 160 – Spring Semester 2015 6 Steps ¡2-­‑4: ¡ Step ¡2 ¡(con?nued) ¡ • S?ll ¡ini?alizing ¡counters… ¡ Tackle ¡each ¡step ¡individually ¡ • – The ¡counters ¡count ¡how ¡oden ¡each ¡leWer ¡appears ¡ – Ini?alize ¡counters ¡ – So ¡we ¡need ¡to ¡ini?alize ¡all ¡26 ¡of ¡them ¡to ¡zero! ¡ Declare ¡the ¡counters ¡first ¡ • – So ¡we ¡use ¡a ¡for ¡loop ¡with ¡26 ¡itera?ons ¡ • But ¡there ¡are ¡26 ¡of ¡them… ¡ • … ¡so ¡use ¡an ¡array! ¡ for(int i=0; i < 26; i++) { ¡ int[] alphaCounters = new int[26]; alphaCounters[i] = 0; } CS 160 – Spring Semester 2015 7 CS 160 – Spring Semester 2015 8 2

  3. Step ¡2 ¡(yet ¡again) ¡ Step ¡2 ¡(one ¡more ¡?me!) ¡ • Actually, ¡hard-­‑coding ¡’26’ ¡is ¡a ¡bad ¡idea ¡ • So ¡pueng ¡it ¡together, ¡we ¡need ¡to ¡declare ¡and ¡ – What ¡if ¡we ¡want ¡to ¡include ¡capitals ¡later? ¡ ini?alize ¡a ¡counter ¡for ¡every ¡leWer ¡in ¡the ¡ alphabet: ¡ – What ¡if ¡we ¡want ¡to ¡re-­‑use ¡this ¡code? ¡ • We ¡want ¡to ¡ini?alize ¡the ¡array, ¡and ¡nothing ¡more: ¡ int[] alphaCounters = new int[26]; int[] alphaCounters = new int[26]; for(int i=0; i < 26; i++) { for (int i=0; i < alphaCounters.length; alphaCounters[i] = 0; i++) { } alphaCounters[i] = 0; } CS 160 – Spring Semester 2015 9 CS 160 – Spring Semester 2015 10 ¡ Does ¡it ¡work? ¡ Prin?ng ¡the ¡Counters ¡ • Did ¡we ¡declare ¡and ¡ini?alize ¡the ¡counters ¡correctly? ¡ • So ¡prin?ng ¡the ¡counters ¡is ¡another ¡loop: ¡ – Never ¡assume ¡something ¡is ¡correct ¡ – Why ¡the ¡trailing ¡println()? ¡ – Test ¡each ¡step ¡before ¡moving ¡on ¡ • How? ¡ – Method ¡#1: ¡Use ¡the ¡debugger ¡ for(int i=0; i < 26; i++) { • Run ¡the ¡code ¡(so ¡far) ¡step-­‑by-­‑step ¡ System.out.print(alphaCounters[i]+”,”); • Check ¡that ¡the ¡array ¡has ¡26 ¡elements ¡ } • Check ¡that ¡each ¡is ¡set ¡to ¡zero ¡ System.out.println(); – Method ¡#2: ¡Write ¡the ¡code ¡to ¡print ¡counters ¡ • Test ¡it ¡before ¡you ¡start ¡coun?ng ¡leWers ¡ CS 160 – Spring Semester 2015 11 CS 160 – Spring Semester 2015 12 3

  4. Your ¡First ¡Test ¡Code ¡ Running ¡Your ¡First ¡Test ¡ • What ¡should ¡this ¡print? ¡ public static void main(String[] args) { // Declare and initialize counters // Declare & Initialize Counters int[] alphaCounters = new int[26]; int[] alphaCounters = new int[26]; for(int i=0; i < 26; i++) { for(int i=0; i < 26; i++) { alphaCounters[i] = 0; alphaCounters[i] = 0; } } // Print Counters // Print Counters for(int i=0; i < 26; i++) { for(int i=0; i < 26; i++) { System.out.print(alphaCounters[i]+”,”); System.out.print(alphaCounters[i]+”,”); } } System.out.println(); System.out.println(); } CS 160 – Spring Semester 2015 13 CS 160 – Spring Semester 2015 14 Reading ¡Strings ¡& ¡Coun?ng ¡ One ¡approach… ¡ LeWers ¡ • The ¡next ¡step ¡has ¡two ¡parts: ¡ • We’ve ¡already ¡wriWen ¡a ¡loop ¡to ¡read ¡un?l ¡“quit” ¡ – Remember ¡the ¡Echo ¡program? ¡ – Read ¡strings ¡(un?l ¡“quit”) ¡ • So ¡let ¡us ¡start ¡with ¡that: ¡ – Increment ¡leWer ¡counters ¡ Scanner in = new Scanner(System. in ); • Decompose ¡the ¡(sub)problem ¡ while (true) { – Either ¡read ¡strings… ¡ String s = in.next(); • Assuming ¡you ¡can ¡count ¡leWers ¡once ¡you ¡have ¡them ¡ if (s.equals(“quit”)) break; – Or ¡count ¡leWer ¡instances ¡ System. out .println(s); // Replace with counting • Given ¡a ¡string ¡(assume ¡you ¡can ¡get ¡strings ¡somehow) ¡ } Don’t try to do two things at once! CS 160 – Spring Semester 2015 15 CS 160 – Spring Semester 2015 16 4

  5. Wait! ¡Stop! ¡Test ¡this ¡First! ¡ Coun?ng ¡LeWers ¡ • How? ¡ • Given ¡a ¡string, ¡we ¡need ¡to ¡increment ¡a ¡counter ¡ for ¡every ¡leWer. ¡ – Use ¡a ¡print ¡statement ¡to ¡make ¡sure ¡you ¡are ¡ geeng ¡the ¡right ¡strings, ¡or… ¡ – Nested ¡loop ¡inside ¡the ¡get ¡string ¡loop ¡ – Use ¡the ¡debugger! ¡ for(int i=0; i < s.length(); i++) { char letter = s.charAt(i); OK, ¡increment ¡le-er ¡count ¡here… ¡ } CS 160 – Spring Semester 2015 17 CS 160 – Spring Semester 2015 18 Coun?ng ¡LeWers ¡(II) ¡ Coun?ng ¡LeWers ¡(alt) ¡ • The ¡previous ¡code ¡is ¡clean ¡but ¡long. ¡ • Now ¡increment ¡the ¡appropriate ¡counter ¡ • Java ¡supports ¡ASCII ¡char ¡subtrac?on ¡ char first_char = ‘a’; for(int i=0; i < s.length(); i++) char second_char = ‘b’; { int offset = second_char – first_char; // 1 char letter = s.charAt(i); for(int i=0; i < s.length(); i++) switch(letter) { { case ‘a’: alphaCounters[0]++; break; char letter = s.charAt(i); case ‘b’: alphaCounters[1]++; break; int offset = letter – ‘a’; case ‘c’: alphaCounters[2]++; break; if ((offset >= 0) && (offset < 26)) { // 23 more cases… alphaCounters[offset]++; } } } CS 160 – Spring Semester 2015 19 CS 160 – Spring Semester 2015 20 5

  6. Put ¡it ¡all ¡together ¡ All ¡together ¡(II) ¡ // Count letters public static void main(String[] args) { while (true) { String s = in.next(); // Declare and initialize counters if (s.equals(“quit”)) break; int[] alphaCounters = new int[26]; for(int i=0; i < s.length(); i++) { for(int i=0; i < 26; i++) { alphaCounters[i] = 0; char letter = s.charAt(i); } int offset = letter – ‘a’; if ((offset >= 0) && (offset < 26)) // Create scanner alphaCounters[offset]++; Scanner in = new Scanner(System. in ); } } CS 160 – Spring Semester 2015 21 CS 160 – Spring Semester 2015 22 All ¡Together ¡(III) ¡ Done ¡Yet? ¡No! ¡ • Test ¡your ¡program: ¡ // Close scanner – Make ¡up ¡some ¡input ¡ in.close(); – Count ¡the ¡leWers ¡by ¡hand ¡ – Double ¡check ¡your ¡program’s ¡results ¡ // Print Counters • Make ¡Hard ¡Tests ¡ for(int i=0; i < 26; i++) { – Include ¡characters ¡that ¡aren’t ¡leWers ¡ System.out.print(alphaCounters[i]+”,”); – Test ¡the ¡case ¡where ¡the ¡first ¡input ¡is ¡‘quit’ ¡ } – Test ¡really ¡long ¡inputs ¡ System.out.println(); – …. ¡ } • Hint: ¡it ¡can ¡be ¡good ¡to ¡think ¡of ¡test ¡cases ¡ first . ¡ } CS 160 – Spring Semester 2015 23 CS 160 – Spring Semester 2015 24 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend