You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch3_javadatatypes.ptx
+11-10Lines changed: 11 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -524,10 +524,10 @@ void main() {
524
524
<title>List</title>
525
525
526
526
<p>
527
-
Next, let’s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file. Here is a simple Python program that creates and prints a histogram.
527
+
Next, let’s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file. <xrefref="python-histogram"text="type-global"/> is a simple Python program that creates and prints a histogram.
Lets review what is happening in this little program. First, we create a list and initialize the first 10 positions in the list to be 0. Next we open the data file called ‘test.dat’. Third, we have a loop that reads each line of the file. As we read each line we convert it to an integer and increment the counter at the position in the list indicated by the number on the line we just read. Finally we iterate over each element in the list, printing out both the position in the list and the total value stored in that position.
@@ -618,7 +619,7 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
618
619
</p>
619
620
620
621
<p>
621
-
Here is the Java code needed to write the exact same program:
622
+
<xrefref="java-histogram"text="type-global"/> is the Java code needed to write the exact same program.
622
623
</p>
623
624
624
625
<note>
@@ -627,7 +628,7 @@ Here is the Java code needed to write the exact same program:
Before going any further, I suggest you try to compile the above program and run it on some test data that you create.
671
+
Before going any further, I suggest you try to compile <xrefref="java-histogram"text="type-global"/> and run it on some test data that you create.
671
672
</p>
672
673
673
674
<p>
674
675
Now, let’s look at what is happening in the Java source. As usual, we declare the variables we are going to use at the beginning of the method. In this example we are declaring a <c>Scanner</c> variable called <c>data</c>, an integer called <c>idx</c> and an <c>ArrayList</c> called <c>count</c>. However, there is a new twist to the <c>ArrayList</c> declaration. Unlike Python where lists can contain just about anything, in Java we let the compiler know what kind of objects our array list is going to contain. In this case the <c>ArrayList</c> will contain <c>Integers</c>. The syntax we use to declare what kind of object the list will contain is the <c><Type></c> syntax.
675
676
</p>
676
677
677
678
<p>
678
-
Technically, you don’t <em>have</em> to declare what is going to be in an array list. The compiler will allow you to leave the <c><``*Type*</c>>`` off the declaration. If you don’t tell Java what kind of object is going to be on the list Java will give you a warning message like this:
679
+
Technically, you don’t <em>have</em> to declare what is going to be in an array list. The compiler will allow you to leave the <c><``*Type*</c>>`` off the declaration. If you don’t tell Java what kind of object is going to be on the list Java will give you a warning message like <xrefref="java-arraylist-warning"text="type-global"/> when you compile the program.
679
680
</p>
680
681
681
682
<listingxml:id="java-arraylist-warning">
@@ -692,7 +693,7 @@ public class Histo {
692
693
</p>
693
694
694
695
<p>
695
-
Lines 13—20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. The following example shows the general structure of a try/catch block.
696
+
Lines 13—20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. <xrefref="java-try-catch"text="type-global"/> shows the general structure of a try/catch block.
0 commit comments