Skip to content

Commit bd65036

Browse files
bowbahdoemasiamj
andauthored
Add arrays section (#22)
* Add start of if/else! ... Co-Authored-By: Mitch Masia <[email protected]> * loops * ... * Update SUMMARY.md * loop challenges * Update challenges.md * .. * Move iteration into loops * More arrays * Update access_individual_elements.md * ... * Close out arrays I sans challenges * Rename features.py to features.java to placate folks Update book.toml Update book.toml * Format --------- Co-authored-by: Mitch Masia <[email protected]>
1 parent ba7cfe0 commit bd65036

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+4119
-126
lines changed

book.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ git-repository-url = "https://github.com/Together-Java/ModernJava"
1010
edit-url-template = "https://github.com/Together-Java/ModernJava/edit/develop/{path}"
1111
mathjax-support = true
1212

13+
[output.html.fold]
14+
enable = true # whether or not to enable section folding
15+
level = 0 # the depth to start folding
16+
1317
[preprocessor.features]
14-
command = "python3 features.py"
18+
command = "python3 features.java"
1519
# Not ready
1620
toplevel_anonymous_class = false
1721
# Not ready

features.py features.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/usr/bin/env python3
22

3+
# This file is python, not Java.
4+
# but we got enough people complaining that the repo in github showed 100% python
5+
# that I just changed the extension to .java - I think its funny. We can replace
6+
# this file if either a json library is added to java or someone wants to do all
7+
# the setup to pull in a json lib.
8+
39
import json
410
import sys
511

src/SUMMARY.md

+56-16
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
- [tutorialspoint](./examples/tutorialspoint.md)
1414
- [tutorialspoint paid course](./examples/tutorialspoint_paid.md)
1515
- [w3schools](./examples/w3schools.md) -->
16-
# Getting Started
17-
18-
- [Hello, World](./getting_started/hello_world.md)
1916

2017
# Modern Java
2118

2219
- [Prelude](./prelude.md)
2320
- [Asking for Help](./prelude/asking_for_help.md)
2421
- [Toy Problems](./prelude/toy_problems.md)
22+
- [Lies](./prelude/lies.md)
23+
- [Getting Started](./getting_started.md)
2524
- [First Steps](./first_steps.md)
2625
- [Comments](./first_steps/comments.md)
2726
- [Semicolons](./first_steps/semicolon.md)
@@ -36,7 +35,7 @@
3635
- [Inferred Types](./variables/inferred_types.md)
3736
- [Challenges](./variables/challenges.md)
3837

39-
# Basic Data Types
38+
# Data Types I
4039

4140
- [Booleans](./boolean.md)
4241
- [And](./boolean/and.md)
@@ -95,21 +94,67 @@
9594

9695
# Control Flow I
9796

98-
- [Branching Logic](./branching_logic.md)
97+
- [Branching Paths](./branching_paths.md)
9998
- [If](./branching_logic/if.md)
10099
- [Else](./branching_logic/else.md)
100+
- [Nested Ifs](./branching_logic/nested_ifs.md)
101101
- [Else If](./branching_logic/else_if.md)
102102
- [Relation to Delayed Assignment](./branching_logic/relation_to_delayed_assignment.md)
103-
- [Ternary Expression](./branching_logic/ternary_expression.md)
103+
- [Conditional Operator](./branching_logic/conditional_operator.md)
104+
- [Boolean Expressions](./branching_logic/boolean_expressions.md)
104105
- [Challenges](./branching_logic/challenges.md)
105106
- [Loops](./loops.md)
106107
- [While](./loops/while.md)
107-
- [Termination Conditions](./loops/termination_conditions.md)
108+
- [Endless Loops](./loops/endless_loops.md)
108109
- [Break](./loops/break.md)
109110
- [Continue](./loops/continue.md)
110-
- [Do](./loops/do.md)
111+
- [Unreachable Code](./loops/unreachable_code.md)
112+
- [Do While](./loops/do_while.md)
113+
- [Nested Loops](./loops/nested_loops.md)
114+
- [Labeled Break](./loops/labeled_break.md)
115+
- [Labeled Continue](./loops/labeled_continue.md)
116+
- [Iteration](./loops/iteration.md)
117+
- [Counting Up](./loops/counting_up.md)
118+
- [Counting Down](./loops/counting_down.md)
119+
- [Iterate over a String](./loops/iterate_over_a_string.md)
111120
- [Challenges](./loops/challenges.md)
112121

122+
# Data Types II
123+
124+
- [Arrays](./arrays.md)
125+
- [Array Initializers](./arrays/array_initializers.md)
126+
- [Length](./arrays/length.md)
127+
- [Access Individual Elements](./arrays/access_individual_elements.md)
128+
- [Set Individual Elements](./arrays/set_individual_elements.md)
129+
- [Aliasing](./arrays/aliasing.md)
130+
- [Reassignment](./arrays/reassignment.md)
131+
- [Relation to Final Variables](./arrays/relation_to_final_variables.md)
132+
- [Printing the Contents of an Array](./arrays/printing_the_contents_of_an_array.md)
133+
- [Empty Array](./arrays/empty_array.md)
134+
- [Difference between Initializer and Literal](./arrays/difference_between_initializer_and_literal.md)
135+
- [Challenges](./arrays/challenges)
136+
137+
# Control Flow II
138+
139+
<!--
140+
- [For](./for.md)
141+
- [Comparison to while]()
142+
- [i](./for/i.md)
143+
- [Relation to Arrays]()
144+
- [var]()
145+
- [final]()
146+
- [For Each]()
147+
148+
- [Iteration]()
149+
- [Iterate over an array]()
150+
151+
- [null](./null.md)
152+
- [Reference Types]()
153+
- [Primitive Types]()
154+
- [Populate array]()
155+
156+
-->
157+
113158
# User Defined Types
114159

115160
- [Classes](./classes.md)
@@ -128,6 +173,7 @@
128173

129174
<!--
130175
Arrays
176+
copy an array (System.arrayCopy, for loop)
131177
Generics
132178
Maven
133179
XML
@@ -242,15 +288,9 @@ Javadoc and Documentation comments
242288
- Getting Started
243289
- [Hello, World]()
244290
- Data Types
245-
- int and long
246-
- Integer Division
247-
- float and double
248-
- Floating Point Numbers
249-
- booleans
291+
250292
- Arrays
251-
- String
252-
- String Literals
253-
- Multiline String Literals
293+
multi dimensional arrays
254294
- Expressions
255295
- Conditionals
256296
- Loops

src/arrays.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Arrays
2+
3+
Arrays are used to represent a fixed-size collection of things.
4+
5+
```java
6+
int[] oddNumbers = { 1, 3, 5, 7, 9 };
7+
```
8+
9+
Fixed-size means that once an array is made, it will always hold the same
10+
number of things.
11+
12+
We call the things stored in an array its "elements."
13+
14+
You can make an array of any type of element by using the name of the type followed by
15+
`[]`.
16+
17+
```java
18+
char[] letters = { 'a', 'b', 'c' };
19+
String[] words = { "its", "as", "easy", "as" }
20+
int[] numbers = { 1, 2, 3 };
21+
double[] doubles = { 97.0, 98.0, 99.0, 1.0, 2.0, 3.0 };
22+
```
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Access Individual Elements
2+
3+
Given an array, you can access any of its elements by index.
4+
5+
You write the name of a variable containing an array, `[`, a number, and then a `]`.
6+
7+
The first element can be accessed by using `0`, the second by using `1`, and so on.
8+
9+
```java
10+
String[] lyrics = { "you", "say", "goodbye" };
11+
12+
String you = lyrics[0];
13+
System.out.println(you);
14+
15+
String say = lyrics[1];
16+
System.out.println(say);
17+
18+
String goodbye = lyrics[2];
19+
System.out.println(goodbye);
20+
```
21+
22+
The index of the element can also come from a variable.
23+
24+
```java
25+
int index = 2;
26+
String[] lyrics = { "I", "say", "hello" };
27+
28+
System.out.println(lyrics[index]);
29+
```
30+
31+
If you give a number equal to or greater than the length of the array or a number less than zero,
32+
you will get an error.
33+
34+
```java
35+
String[] lyrics = { "I", "say", "hi" };
36+
// Crash!
37+
lyrics[999];
38+
```
39+
40+
```java
41+
String[] lyrics = { "you", "say", "low" };
42+
// Crash!
43+
lyrics[-1];
44+
```

src/arrays/aliasing.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Aliasing
2+
3+
When you assign a variable containing an array to another variable, the array
4+
referenced by both variables will be the exact same.
5+
6+
This means that if the contents of the array are updated, that change will
7+
be reflected by both variables.
8+
9+
```java
10+
char[] lettersOne = { 'B', 'a', 't', 'm', 'a', 'n' };
11+
char[] lettersTwo = lettersOne;
12+
13+
// Batman
14+
System.out.println(lettersOne);
15+
// Batman
16+
System.out.println(lettersTwo);
17+
18+
lettersOne[0] = 'C';
19+
20+
// Catman
21+
System.out.println(lettersOne);
22+
// Catman
23+
System.out.println(lettersTwo);
24+
25+
lettersTwo[0] = 'R';
26+
27+
// Ratman
28+
System.out.println(lettersOne);
29+
// Ratman
30+
System.out.println(lettersTwo);
31+
```
32+
33+
When two variables point to the same thing like this we say that both variables are "aliases"
34+
for eachother.

src/arrays/array_initializers.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Array Initializers
2+
3+
To give an initial value to an array you can use an array initializer.
4+
5+
After the equals sign you write `{` followed by a comma separated list of elements and a final `}`.
6+
7+
```java
8+
int[] numbers = { 1, 2, 3 }
9+
// |---------|
10+
// this part is
11+
// the initializer
12+
```
13+
14+
The elements in an initializer do not have to be literals and can also be variables or expressions.
15+
16+
```java
17+
int two = 2;
18+
// Will hold 1, 2, 3 just like the array above
19+
int[] numbers = { 1, two, two + 1 }
20+
```
21+
22+
We call them array initializers because you use them to give an initial value to an array.[^pattern]
23+
24+
[^pattern]: You may be noticing a pattern. Confusing sounding names are often kinda "obvious"
25+
with enough context.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Difference between Initializer and Literal
2+
3+
The reason `{ 1, 2, 3 }` is called an "array initializer" and not
4+
an "array literal" is somewhat subtle.
5+
6+
When you have a literal, like a `String` literal, you can assign that to a variable and
7+
then use that `String` afterwards.
8+
9+
```java
10+
String name = "Alana";
11+
// l
12+
System.out.println(name.charAt(1));
13+
```
14+
15+
But you can also perform those operations using the literal itself, without an intermediate variable.
16+
17+
```java
18+
// l
19+
System.out.println("Alana".charAt(1));
20+
```
21+
22+
Array initializers work in the case where you first assign them to a variable before using
23+
the array.
24+
25+
```java
26+
char[] name = { 'A', 'm', 'a', 'n', 'd', 'a' };
27+
// m
28+
System.out.println(name[1]);
29+
```
30+
31+
But they do not work to perform operations on directly.
32+
33+
```java
34+
// Will not run
35+
System.out.println({ 'A', 'm', 'a', 'n', 'd', 'a' }[1]);
36+
```

src/arrays/empty_array.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Empty Array
2+
3+
If you use an array initializer that has no elements between the `{` and `}`
4+
you can create an empty array.
5+
6+
```java
7+
char[] emptyCharArray = {};
8+
```
9+
10+
An empty array is very similar to an empty `String`. It has a length of 0, it has no elements,
11+
and it is generally useful only as a placeholder value for when you have no data yet but will
12+
be able to reassign the variable holding it when you get some.
13+
14+
```java
15+
char[] emptyCharArray = {};
16+
17+
// 0
18+
System.out.println(emptyCharArray.length);
19+
20+
// Crash
21+
System.out.println(emptyCharArray[0]);
22+
```

src/arrays/length.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Length
2+
3+
The number of elements which comprise an array can be accessed by using `.length`.[^unlike_string]
4+
5+
```java
6+
String[] veggies = { "brussels", "cabbage", "carrots" };
7+
int numberOfElements = veggies.length;
8+
9+
// veggies is 3 elements long
10+
System.out.println(
11+
"veggies is " numberOfElements + " characters long"
12+
);
13+
```
14+
15+
[^unlike_string]: Unlike with a `String`, you do not write `()` after `.length`.

0 commit comments

Comments
 (0)