-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch07.tex
More file actions
1179 lines (912 loc) · 38.6 KB
/
ch07.tex
File metadata and controls
1179 lines (912 loc) · 38.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Arrays and references}
Up to this point, the only variables we have used were for individual values such as numbers or strings.
In this chapter, we'll learn how to store multiple values of the same type using a single variable.
This language feature will enable you to write programs that manipulate larger amounts of data.
For example, Exercise~\ref{doubloon} asked you to check whether every letter in a string appears exactly twice.
One algorithm (which hopefully you already discovered) is to loop through the string 26 times, once for each lowercase letter:
\begin{code}
for (char c = 'a'; c <= 'z'; c++) {
// count how many times the letter appears
// if the count is not 0 or 2, return false
}
\end{code}
This ``nested loops'' approach is inefficient, especially when the string is long (e.g., one billion characters).
Another algorithm would initialize 26 variables to zero, loop through the string once, and use a giant \java{if} statement update the variable for each letter.
But who wants to declare 26 variables?
That's where arrays come in.
We can declare a single variable that stores 26 integers.
Rather than use an \java{if} statement to update each value, we can use arithmetic to update the $n$th value directly.
We will present this algorithm at the end of the chapter.
\section{Creating arrays}
\index{array}
\index{element}
An {\bf array} is a sequence of values; the values in the array are called {\bf elements}.
You can make an array of \java{int}s, \java{double}s, \java{String}s, or any other type, but all the values in an array must have the same type.
\index{type!array}
\index{[ ] square brackets}
\index{brackets!square}
To create an array, you have to declare a variable with an {\em array type} and then create the array itself.
Array types look like other Java types, except they are followed by square brackets (\java{[]}).
For example, the following lines declare that \java{counts} is an ``integer array'' and \java{values} is a ``double array'':
\begin{code}
int[] counts;
double[] values;
\end{code}
\index{new}
\index{operator!new}
\index{allocate}
To create the array itself, you have to use the \java{new} operator, which we first saw in Section~\ref{scanner}.
The \java{new} operator {\bf allocates} memory for the array and automatically initializes all of its elements to zero.
\begin{code}
counts = new int[4];
values = new double[size];
\end{code}
The first assignment makes \java{counts} refer to an array of four integers.
The second makes \java{values} refer to an array of \java{double}s, but the number of elements depends on the value of \java{size} (at the time the array is created).
Of course, you can also declare the variable and create the array with a single line of code:
\begin{code}
int[] counts = new int[4];
double[] values = new double[size];
\end{code}
\index{NegativeArraySizeException}
\index{exception!NegativeArraySize}
You can use any integer expression for the size of an array, as long as the value is nonnegative.
If you try to create an array with \java{-4} elements, for example, you will get a \java{NegativeArraySizeException}.
An array with zero elements is allowed, and there are special uses for such arrays that we'll see later on.
\section{Accessing elements}
\label{elements}
When you create an array with the \java{new} operator, the elements are initialized to zero.
Figure~\ref{fig.array} shows a memory diagram of the \java{counts} array so far.
\begin{figure}[!ht]
\begin{center}
\includegraphics{figs/array.pdf}
\caption{Memory diagram of an \java{int} array.}
\label{fig.array}
\end{center}
\end{figure}
\index{reference}
The arrow indicates that the value of \java{counts} is a {\bf reference} to the array.
You should think of {\em the array} and {\em the variable} that refers to it as two different things.
As we'll soon see, we can assign a different variable to refer to the same array, and we can change the value of \java{counts} to refer to a different array.
\index{element}
\index{index}
\index{array!element}
\index{array!index}
The large numbers inside the boxes are the elements of the array.
The small numbers outside the boxes are the {\bf indexes} used to identify each location in the array.
As with strings, the index of the first element is 0, not 1.
For this reason, we sometimes refer to the first element as the ``zeroth'' element.
The \java{[]} operator selects elements from an array:
\begin{code}
System.out.println("The zeroth element is " + counts[0]);
\end{code}
You can use the \java{[]} operator anywhere in an expression:
\begin{code}
counts[0] = 7;
counts[1] = counts[0] * 2;
counts[2]++;
counts[3] -= 60;
\end{code}
Figure~\ref{fig.array2} shows the result of these statements.
\begin{figure}[!ht]
\begin{center}
\includegraphics{figs/array2.pdf}
\caption{Memory diagram after several assignment statements.}
\label{fig.array2}
\end{center}
\end{figure}
You can use any expression as an index, as long as it has type \java{int}.
One of the most common ways to index an array is with a loop variable.
For example:
\begin{code}
int i = 0;
while (i < 4) {
System.out.println(counts[i]);
i++;
}
\end{code}
This \java{while} loop counts up from 0 to 4.
When \java{i} is 4, the condition fails and the loop terminates.
So the body of the loop is only executed when \java{i} is 0, 1, 2, and 3.
In this context, the variable name \java{i} is short for ``index''.
\index{loop variable}
\index{variable!loop}
Each time through the loop we use \java{i} as an index into the array, displaying the \java{i}th element.
This type of array processing is usually written as a \java{for} loop.
\begin{code}
for (int i = 0; i < 4; i++) {
System.out.println(counts[i]);
}
\end{code}
\index{ArrayIndexOutOfBoundsException}
\index{exception!ArrayIndexOutOfBounds}
For the \java{counts} array, the only legal indexes are 0, 1, 2, and 3.
If the index is negative or greater than 3, the result is an \java{ArrayIndexOutOfBoundsException}.
\section{Displaying arrays}
\label{printarray}
\index{array!printing}
You can use \java{println} to display an array, but it probably doesn't do what you would like.
%slr: 8-21-19
For example, the following trinket program (1) declares an array variable, (2) makes it refer to an array of four elements with an \textbf{array initializer}, and (3) attempts to display the contents of the array using \java{println}:
%
%\begin{code}
%int[] a = {1, 2, 3, 4};
%System.out.println(a);
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [230] {PrintArrayReference.java}
public class PrintArrayReference {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4}; //array initializer
/* above statement is equivalent to the following
int[] a = new int[4];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4; */
System.out.println(a); //a is a *refernce* to an array
}
}
\end{trinket}
Run the program to see what you get. Unfortunately, the output is something like:
%slr: end 8-21-19
\begin{stdout}
[I@bf3f7e0
\end{stdout}
The bracket indicates that the value is an array, \java{I} stands for ``integer'', and the rest represents the address of the array in memory.
If we want to display the elements of the array, we could do it ourselves:
%slr: 8-21-19
%\begin{code}
%public static void printArray(int[] a) {
% System.out.print("{" + a[0]);
% for (int i = 1; i < a.length; i++) {
% System.out.print(", " + a[i]);
% }
% System.out.println("}");
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [190] {PrintArrayValues.java}
public class PrintArrayValues {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4}; //array initializer
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
\end{trinket}
%Given the previous array, the output of \java{printArray} is:
%
%\begin{stdout}
%{1, 2, 3, 4}
%\end{stdout}
Run the program and see what you get.
In the example above, we see that \java{a.length} gives the length of the array. The pattern of the for loop is important to understand. The \textbf{length} of \java{a} is \textbf{4}, but the \textbf{indexes} are \java{0, 1, 2} and \java{3}. The for loop starts the loop index \java{i} at \java{0}, and continues as long as \java{i < a.length} giving \java{i} four values, namely \java{0, 1, 2} and \java{3}, precisely what we want.
%slr: end 8-21-19
\index{utility class}
\index{Arrays class}
The Java library provides a utility class \java{java.util.Arrays} that has methods for working with arrays.
One of them, \java{toString}, returns a string representation of an array.
We can invoke it like this:
\begin{code}
System.out.println(Arrays.toString(a));
\end{code}
And the output is:
\begin{stdout}
[1, 2, 3, 4]
\end{stdout}
As usual, we have to \java{import java.util.Arrays} before we can use it.
Notice that the string format is slightly different: it uses square brackets instead of curly braces.
But it beats having to write your own \java{printArray} method.
%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{PrintArrayValues} program in the trinket above so as to use the \java{Arrays.toSting()} method to print the values of \java{a}.
\item Change the initializer on line 4. Intialize with more, different values and see what happens.
\item Change the intiializer again, this time adding a \java{double} literal, say 3.5. What happens?
\end{enumerate}
%slr: end 8-16-19
\section{Copying arrays}
\index{array!copying}
As explained in Section~\ref{elements}, array variables contain {\em references} to arrays.
When you make an assignment to an array variable, it simply copies the reference.
But it doesn't copy the array itself.
For example:
%slr: 8-21-19
%\begin{code}
%double[] a = new double[3];
%double[] b = a;
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [250] {ArrayAliases.java}
import java.util.Arrays;
public class ArrayAliases {
public static void main(String[] args) {
double[] a = new double[3];
double[] b = a;
System.out.printf("a : %s\n", Arrays.toString(a));
System.out.printf("b : %s\n", Arrays.toString(b));
a[0] = 17.0;
System.out.printf("a : %s\n", Arrays.toString(a));
System.out.printf("b : %s\n", Arrays.toString(b));
}
}
\end{trinket}
Run the program to see what happens. Changing \java{a[0]} also changed \java{b[0]}!
%slr: end 8-21-19
These statements create an array of three \java{double}s and make two different variables refer to it, as shown in Figure~\ref{fig.array3}.
\index{memory diagram}
\index{diagram!memory}
\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.75]{figs/array3.pdf}
\caption{Memory diagram of two variables referring to the same array.}
\label{fig.array3}
\end{center}
\end{figure}
\index{alias}
Any changes made through either variable will be seen by the other.
Above, we set \java{a[0] = 17.0}, and then displayed \java{b[0]}, and got {\tt 17.0}.
Because \java{a} and \java{b} are different names for the same thing, they are sometimes called {\bf aliases}.
If you actually want to copy the array, not just the reference, you have to create a new array and copy the elements from one to the other, like this:
%slr: 8-21-19
%\begin{code}
%double[] b = new double[3];
%for (int i = 0; i < 3; i++) {
% b[i] = a[i];
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [320] {CopyArray.java}
import java.util.Arrays;
public class CopyArray {
public static void main(String[] args) {
double[] a = {17, 0, 0};
double[] b = new double[a.length];
for (int i = 0; i < a.length; i++){
b[i] = a[i];
}
System.out.printf("a : %s\n", Arrays.toString(a));
System.out.printf("b : %s\n", Arrays.toString(b));
a[1] = Math.PI;
System.out.printf("a : %s\n", Arrays.toString(a));
System.out.printf("b : %s\n", Arrays.toString(b));
}
}
\end{trinket}
Run the program to see what you get. Now the arrays are not aliases, but are independent arrays, each of length \java{3}.
\index{Arrays class}
\java{java.util.Arrays} provides a method named \java{copyOf} that performs this task for you.
So you can replace the previous code with one line:
\begin{code}
double[] b = Arrays.copyOf(a, a.length);
\end{code}
The second parameter is the number of elements you want to copy, so \java{copyOf} can also be used to copy part of an array.
Figure~\ref{fig.array4} shows the state of the array variables after copying the arrays, either iteratively or by invoking \java{Arrays.copyOf}.
\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.75]{figs/array4.pdf}
\caption{Memory diagram of two variables referring to different arrays.}
\label{fig.array4}
\end{center}
\end{figure}
When the program above changed the value of \java{a[1]}, the corresponding value \java{b[1]} was unchanged.
%\section{Array length}
\index{length!array}
\index{array!length}
%slr: 8-21-19
%The examples so far only work if the array has three elements.
%It is better to generalize the code to work with arrays of any size.
%We can do that by replacing the magic number, \java{3}, with \java{a.length}:
%
%\begin{code}
%double[] b = new double[a.length];
%for (int i = 0; i < a.length; i++) {
% b[i] = a[i];
%}
%\end{code}
%slr: end 8-21-19
All arrays have a built-in constant, \java{length}, that stores the number of elements.
In contrast to \java{String.length()}, which is a method, \java{a.length} is a constant.
The expression \java{a.length} may look like a method invocation, but there are no parentheses and no arguments.
The last time the loop gets executed, \java{i} is \java{a.length - 1}, which is the index of the last element.
When \java{i} is equal to \java{a.length}, the condition fails and the body is not executed -- which is a good thing, because trying to access \java{a[a.length]} would throw an exception.
Of course we can replace the loop altogether by using \java{Arrays.copyOf} and \java{a.length} for the second argument.
The following line produces the same result shown in Figure~\ref{fig.array4}.
\begin{code}
double[] b = Arrays.copyOf(a, a.length);
\end{code}
%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{CopyArray} program in the trinket above so as to use the \java{Arrays.copyOf()} method to make the copy of \java{a}.
\item Modify the call to copy only the first two values of a into b, making \java{b.length} equal to 2.
\end{enumerate}
%slr: end 8-21-19
\section{Array traversal}
\label{traversal}
\index{traversal}
Many computations can be implemented by looping through the elements of an array and performing an operation on each element.
Looping through the elements of an array is called a {\bf traversal}.
\begin{code}
int[] a = {1, 2, 3, 4, 5};
for (int i = 0; i < a.length; i++) {
a[i] *= a[i];
}
\end{code}
This example traverses an array and squares each element.
At the end of the loop, the array has the values \java{{1, 4, 9, 16, 25}}.
\index{search}
Another common pattern is a {\bf search}, which involves traversing an array and ``searching'' for a particular element.
For example, the following program invokes a method that takes an array and a value, and returns the index where the value appears:
%slr: 8-21-19
\begin{code}
public static int search(double[] array, double target) {
for (int i = 0; i < array.length; i++) {
if (array[i] == target) {
return i;
}
}
return -1; // not found
}
\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [370] {SearchArray.java}
public class SearchArray {
public static int search( double[] array, double target) {
for (int i = 0; i < array.length; i++) {
if (array[i] == target) {
return i;
}
}
return -1; // not found
}
public static void main(String[] args) {
double[] a = {17.5, -3.2, 8, Math.E};
double x = -3.2;
int index = search(a, x);
if (index >= 0) {
System.out.printf("%f found in array at index %d\n", x, index);
} else {
System.out.printf("%f not round in array\n");
}
}
}
\end{trinket}
%slr: end 8-21-19
If we find the target value in the array, the \java{search} method returns its index immediately.
If the loop exits without finding the target, it returns \java{-1}, a special value chosen to indicate a failed search.
(This code is essentially what the \java{String.indexOf} method does.)
Run the program with a few different values of \java{x} to see how it behaves when it does and does not find the value in the array.
%slr: 8-21-19
%The following code searches an array for the value \java{1.23}, which is the third element.
%Because array indexes start at zero, the output is \java{2}.
%
%\begin{code}
%double[] array = {3.14, -55.0, 1.23, -0.8};
%int index = search(array, 1.23);
%System.out.println(index);
%\end{code}
%slr: end 8-21-19
\index{reduce}
Another common traversal is a {\bf reduce} operation, which ``reduces'' an array of values down to a single value.
Examples include the sum or product of the elements, the minimum, and the maximum.
The following program uses a method named \java{sum} that takes an array and returns the sum of its elements:
%slr: 8-21-19
%\begin{code}
%public static double sum(double[] array) {
% double total = 0.0;
% for (int i = 0; i < array.length; i++) {
% total += array[i];
% }
% return total;
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [320] {SumArray.java}
public class SumArray {
public static double sum(double[] array) {
double total = 0.0; // 'accumulator'
for (int i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}
public static void main(String[] args) {
double[] a = {17.5, -3.2, 8, Math.E};
System.out.println("The sum of the array elements is " + sum(a));
}
}
\end{trinket}
%slr: end 8-21-19
Run the program and see how it works.
\index{accumulator}
In the method \java{sum}, before the loop, we initialize \java{total} to zero.
Each time through the loop, we update \java{total} by adding one element from the array.
At the end of the loop, \java{total} contains the sum of the elements.
A variable used this way is sometimes called an {\bf accumulator}, because it ``accumulates'' the running total.
%slr: 8-21-19
\index{programming idiom}
Note that each of these array traversals employs the same for loop structure:
\begin{code}
for (int i = 0; i < array.length; i++) {
//...
}
\end{code}
This is a \textbf{very} common Java \href{https://en.wikipedia.org/wiki/Programming_idiom}{programming idiom}.
%slr: end 8-21-19
\section{Random numbers}
\label{random}
\index{deterministic}
Most computer programs do the same thing every time they run; programs like that are called {\bf deterministic}.
Usually determinism is a good thing, since we expect the same calculation to yield the same result.
But for some applications, we want the computer to be unpredictable.
Games are an obvious example, but there are many others, like scientific simulations.
%Technically speaking, all computer programs are deterministic: they simply execute the source code.
\index{nondeterministic}
\index{pseudorandom}
Making a program {\bf nondeterministic} turns out to be hard, because it's impossible for a computer to generate truly random numbers.
But there are algorithms that generate unpredictable sequences called {\bf pseudorandom} numbers.
For most applications, they are as good as random.
%Nondeterminism is a theoretical concept for analyzing the complexity of algorithms.
\index{Random}
\index{nextInt!Random}
If you did Exercise~\ref{guess}, you have already seen \java{java.util.Random}, which generates pseudorandom numbers.
The method \java{nextInt} takes an integer argument, \java{n}, and returns a random integer between \java{0} and \java{n - 1} (inclusive).
If you generate a long series of random numbers, every value should appear, at least approximately, the same number of times.
One way to test this behavior of \java{nextInt} is to generate a large number of values, store them in an array, and count the number of times each value occurs.
The following method creates an \java{int} array and fills it with random numbers between 0 and 99.
The argument specifies the desired size of the array, and the return value is a reference to the new array.
\begin{code}
public static int[] randomArray(int size) {
Random random = new Random();
int[] a = new int[size];
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
return a;
}
\end{code}
%slr: 8-21-19
%The following \java{main} method generates an array and displays it using \java{printArray} from Section~\ref{printarray}.
%We could have used \java{Arrays.toString}, but we like seeing curly braces instead of square brackets.
%
%\begin{code}
%public static void main(String[] args) {
% int[] array = randomArray(8);
% printArray(array);
%}
%\end{code}
%
%Each time you run the program, you should get different values.
%The output will look something like this:
%
%\begin{stdout}
%{15, 62, 46, 74, 67, 52, 51, 10}
%\end{stdout}
The following program generates an array of pseudo-random numbers using the method \java{randomArray} and displays it:
% height = 130 + 11 * num_lines
\begin{trinket} [320] {RandomArray.java}
import java.util.Arrays;
import java.util.Random;
public class RandomArray {
public static int[] randomArray(int size) {
Random random = new Random();
int[] a = new int[size];
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
return a;
}
public static void main(String[] args) {
int[] array = randomArray(8);
System.out.println(Arrays.toString(array));
}
}
\end{trinket}
%slr: end 8-21-19
Run it a few times to get a feel for how the values change and what their range is.
%slr: end 8-21-19
\section{Building a histogram}
\label{singlepass}
\index{histogram}
\index{counter}
If these values were exam scores -- and they would be pretty bad exam scores in that case -- the teacher might present them to the class in the form of a {\bf histogram}.
In statistics, a histogram is a set of counters that keeps track of the number of times each value appears.
For exam scores, we might have ten counters to keep track of how many students scored in the 90s, the 80s, etc.
To do that, we can traverse the array and count the number of elements that fall in a given range.
The following method takes an array and two integers.
It returns the number of elements that fall in the range from \java{low} to \java{high - 1}.
\begin{code}
public static int inRange(int[] a, int low, int high) {
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] >= low && a[i] < high) {
count++;
}
}
return count;
}
\end{code}
\index{reduce}
This pattern should look familiar: it is another reduce operation.
Notice that \java{low} is included in the range (\java{>=}), but \java{high} is excluded (\java{<}).
This design keeps us from counting any scores twice.
Now we can count the number of scores in each grade range.
We add the following code to our \java{main} method:
\begin{code}
int[] scores = randomArray(30);
int a = inRange(scores, 90, 100);
int b = inRange(scores, 80, 90);
int c = inRange(scores, 70, 80);
int d = inRange(scores, 60, 70);
int f = inRange(scores, 0, 60);
\end{code}
This code is repetitive, but it is acceptable as long as the number of ranges is small.
Suppose we wanted to keep track of the number of times each individual score appears.
Then we would have to write 100 lines of code:
\begin{code}
int count0 = inRange(scores, 0, 1);
int count1 = inRange(scores, 1, 2);
int count2 = inRange(scores, 2, 3);
...
int count99 = inRange(scores, 99, 100);
\end{code}
What we need is a way to store 100 counters, preferably so we can use an index to access them.
Wait a minute, that's exactly what an array does.
The following fragment creates an array of 100 counters, one for each possible score.
It loops through the scores and uses \java{inRange} to count how many times each score appears.
Then it stores the results in the \java{counts} array:
\begin{code}
int[] counts = new int[100];
for (int i = 0; i < counts.length; i++) {
counts[i] = inRange(scores, i, i + 1);
}
\end{code}
Notice that we are using the loop variable \java{i} three times: as an index into the \java{counts} array, and in the last two arguments of \java{inRange}.
%slr: 8-21-19
Lets put this together and see how it works. Remember we are trying to see how well our pseudo-random number generator performs - each value should appear around once - maybe 0 or two times here and there. But with 100 values generated in the interval [0, 99] we would not expect to see, say, the value 7 repeated 25 times.
% height = 130 + 11 * num_lines
\begin{trinket} [500] {Histogram1.java}
import java.util.Random;
import java.util.Arrays;
public class Histogram1 {
public static int[] randomArray(int size) {
Random random = new Random();
int[] a = new int[size];
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
return a;
}
public static int inRange(int[] a, int low, int high) {
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] >= low && a[i] < high) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] scores = randomArray(100); //100 values in range [0,99]
int[] counts = new int[100];
for (int i = 0; i < counts.length; i++) {
counts[i] = inRange(scores, i, i+1);
}
System.out.println(Arrays.toString(counts));
}
}
\end{trinket}
Run it and see how well the pseudo-random number generator seems to be working.
%slr: end 8-21-19
\index{efficiency}
The code works, but it is not as efficient as it could be.
Every time the loop invokes \java{inRange}, it traverses the entire array.
It would be better to make a single pass through the \java{scores} array.
For each score, we already know which range it falls in -- the score itself.
We can use that value to increment the corresponding counter, and discard the method \java{inRange}.
This code traverses the array of scores {\em only once} to generate the histogram:
%slr: 8-21-19
%\begin{code}
%int[] counts = new int[100];
%for (int i = 0; i < scores.length; i++) {
% int index = scores[i];
% counts[index]++;
%}
%\end{code}
% height = 130 + 11 * num_lines
\begin{trinket} [400] {Histogram2.java}
import java.util.Random;
import java.util.Arrays;
public class Histogram2 {
public static int[] randomArray(int size) {
Random random = new Random();
int[] a = new int[size];
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
return a;
}
public static void main(String[] args) {
int[] scores = randomArray(100); //100 values in range [0,99]
int[] counts = new int[100];
for (int i = 0; i < counts.length; i++) {
int index = scores[i];
counts[index]++;
}
System.out.println(Arrays.toString(counts));
}
}
\end{trinket}
Each time through the loop, it selects one element from \java{scores} and uses it as an index to increment the corresponding element of \java{counts}.
Because this code traverses the array of scores only once, it is much more efficient.
%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{Histogram2} program in the trinket above so as to generate 1000 values in the range [0, 99] and build a histogram.
\item Modify the \java{Histogram1} program (the one further up that uses the \java{inRange} method) to likewise generate 1000 values in the range [0, 99] and count how many values ended up in the 10 intervals [0, 10), [10, 20), ..., [90, 100). Does the pseudo-random number generator seem to be working well?
\end{enumerate}
%slr: end 8-21-19
\section{The enhanced for loop}
\label{enhanced}
Since traversing arrays is so common, Java provides an alternative syntax that makes the code more compact.
Consider a \java{for} loop that displays the elements of an array on separate lines:
\begin{code}
for (int i = 0; i < values.length; i++) {
int value = values[i];
System.out.println(value);
}
\end{code}
We could rewrite the loop like this:
\begin{code}
for (int value : values) {
System.out.println(value);
}
\end{code}
\index{enhanced for loop}
\index{for!enhanced}
This statement is called an {\bf enhanced for loop}, also known as the ``for each'' loop.
You can read the code as, ``for each \java{value} in \java{values}''.
It's conventional to use plural nouns for array variables and singular nouns for element variables.
Notice how the single line \java{for (int value : values)} replaces the first two lines of the standard \java{for} loop.
It hides the details of iterating each index of the array, and instead, focuses on the values themselves.
Using the enhanced \java{for} loop, and removing the temporary variable, we can write the histogram code from the previous section more concisely:
\begin{code}
int[] counts = new int[100];
for (int score : scores) {
counts[score]++;
}
\end{code}
Enhanced \java{for} loops often make the code more readable, especially for accumulating values.
But they are not helpful when you need to refer to the index, as in search operations.
\begin{code}
for (double d : array) {
if (d == target) {
// array contains d, but we don't know where
}
}
\end{code}
%slr: 8-21-19
\textbf{Section Exercises}
\begin{enumerate}
\item Modify the \java{Histogram2} program in the trinket above so as to use the for each loop instead of a for loop. It should work the same. Does it?
\end{enumerate}
%slr: end 8-21-19
\section{Counting characters}
We now return to the example from the beginning of the chapter and present a solution to Exercise~\ref{doubloon} using arrays.
Here is the problem again:
\begin{quote}
A word is said to be a ``doubloon'' if every letter that appears in the word appears exactly twice.
Write a method called \java{isDoubloon} that takes a string and checks whether it is a doubloon.
To ignore case, invoke the \java{toLowerCase} method before checking.
\end{quote}
Based on the approach from Section~\ref{singlepass}, we will create an array of 26 integers to count how many times each letter appears.
We convert the string to lowercase, so that we can treat \java{'A'} and \java{'a'} (for example) as the same latter.
\begin{code}
int[] counts = new int[26];
String lower = s.toLowerCase();
\end{code}
We can use a \java{for} loop to iterate each character in the string.
To update the \java{counts} array, we need to compute the index that corresponds to each character.
Fortunately, Java allows you to perform arithmetic on characters.
\begin{code}
for (int i = 0; i < lower.length(); i++) {
char letter = lower.charAt(i);
int index = letter - 'a';
counts[index]++;
}
\end{code}
\index{toCharArray}
To simplify the code, it would be nice to use an enhanced \java{for} loop.
The enhanced \java{for} loop does not work with strings directly, but you can convert any string to a character array and iterate that instead:
\begin{code}
for (char letter : lower.toCharArray()) {
int index = letter - 'a';
counts[index]++;
}
\end{code}
After counting all the characters in the \java{lower} string, we need one last \java{for} loop to determine whether each letter appears 0 or 2 times.
\begin{code}
for (int count : counts) {
if (count != 0 && count != 2) {
return false; // not a doubloon
}
}
return true; // is a doubloon
\end{code}
Like in Section~\ref{traversal}, we can return immediately if the inner condition is true (which, in this example, means that the word is not a doubloon).
If we make it all the way through the \java{for} loop, we know that all counts are 0 or 2.
Pulling together the code fragments, and adding some comments and test cases, here is an entire program.
%This example uses \java{if} statements, \java{for} loops, methods, strings, and arrays.
It's amazing to think about how much you've learned in just seven chapters!
\index{Doubloon.java}
\begin{trinket}{Doubloon.java}
public class Doubloon {
public static boolean isDoubloon(String s) {
// count the number of times each letter appears
int[] counts = new int[26];
String lower = s.toLowerCase();
for (char letter : lower.toCharArray()) {
int index = letter - 'a';
counts[index]++;
}
// determine whether the given word is a doubloon
for (int count : counts) {
if (count != 0 && count != 2) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isDoubloon("Mama")); // true
System.out.println(isDoubloon("Lama")); // false
}
}
\end{trinket}
\section{Vocabulary}
\begin{description}
\term{array}
A collection of values, where all the values have the same type, and each value is identified by an index.
\term{element}
One of the values in an array.
The \java{[]} operator selects elements.
\term{index}
An integer variable or value used to indicate an element of an array.
\term{allocate}
To reserve memory for an array or other object.
In Java, the \java{new} operator allocates memory.
\term{reference}
A value that indicates a storage location.
In a memory diagram, a reference appears as an arrow.
\term{alias}
A variable that refers to the same object as another variable.
\term{traversal}
Looping through the elements of an array (or other collection).