Skip to content

Commit

Permalink
Patch up arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Sep 22, 2024
1 parent ac384a6 commit a577e3f
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
- [Printing the Contents of an Array](./arrays/printing_the_contents_of_an_array.md)
- [Empty Array](./arrays/empty_array.md)
- [Difference between Initializer and Literal](./arrays/difference_between_initializer_and_literal.md)
- [Initialization with new](./arrays/initialization_with_new.md)
- [Challenges](./arrays/challenges.md)

# Control Flow II
Expand Down
8 changes: 8 additions & 0 deletions src/arrays/difference_between_initializer_and_literal.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,38 @@ When you have a literal, like a `String` literal, you can assign that to a varia
then use that `String` afterwards.

```java
~void main() {
String name = "Alana";
// l
System.out.println(name.charAt(1));
~}
```

But you can also perform those operations using the literal itself, without an intermediate variable.

```java
~void main() {
// l
System.out.println("Alana".charAt(1));
~}
```

Array initializers work in the case where you first assign them to a variable before using
the array.

```java
~void main() {
char[] name = { 'A', 'm', 'a', 'n', 'd', 'a' };
// m
System.out.println(name[1]);
~}
```

But they do not work to perform operations on directly.

```java
~void main() {
// Will not run
System.out.println({ 'A', 'm', 'a', 'n', 'd', 'a' }[1]);
~}
```
39 changes: 39 additions & 0 deletions src/arrays/initialization_with_new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Initialization with new

Before the initializer for an array, you are allowed to write
`new` followed by a space, the type of thing in the array,
and an empty `[]`.

```java
~void main() {
char[] mainCharacter = { 'A', 'a', 'n', 'g' };
System.out.println(mainCharacter);

char[] sideCharacter = new char[] { 'A', 'a', 'n', 'g' };
System.out.println(sideCharacter);
~}
```

This is required for performing delayed initialization of a variable
holding an array.

```java
~void main() {
char[] element;

element = new char[] { 'f', 'i', 'r', 'e' };
System.out.println(element);

// This would not work
// element = { 'f', 'i', 'r', 'e' };
~}
```

One ability this gives you is to use an array in an expression. I.E.
the initializer coupled with the `new char[]` is akin to an "array expression."

```java
~void main() {
System.out.println(new char[]{ 'K', 'a', 't', 'a', 'r', 'a' }[1]);
~}
```
1 change: 0 additions & 1 deletion src/arrays/initialization_without_initializer

This file was deleted.

Empty file.
6 changes: 4 additions & 2 deletions src/arrays/printing_the_contents_of_an_array.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ If you want to actually see the contents of an array, you should
use a loop.[^future]

```java
~void main() {
String[] factions = { "empire", "stormcloaks", "dragons" };

int index = 0;
while (index < factions.length) {
System.out.println(factions[index])
index++
System.out.println(factions[index]);
index++;
}
~}
```

[^gibberish]: What `[I@5a07e868` and co. mean isn't really important. Try not to get too distracted by it.
Expand Down
26 changes: 26 additions & 0 deletions src/boxed_primitives/arrays_of_boxed_primitives.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,31 @@ numbersOne = numbersTwo;
numbersTwo = numbersOne;
```

This means that to turn something like a `boolean[]` into a `Boolean[]` or vice-versa,
you must manually make a new array and copy over elements. Doing this in either
direction will work because boxing and unboxing conversions exist between the primitives
and their boxed variants.

```java
~void main() {
boolean[] yesAndNo = new boolean[] { true, false };

Boolean[] yesAndNoCopy = new Boolean[] { false, false };
for (int i = 0; i < yesAndNo.length; i++) {
// Here a boxing conversion takes place
yesAndNoCopy[i] = yesAndNo[i];
}

boolean[] yesAndNoCopyCopy = new boolean[] { false, false };
for (int i = 0; i < yesAndNoCopy.length; i++) {
// And here an unboxing conversion
yesAndNoCopyCopy[i] = yesAndNoCopy[i];
}
~}
```




[^interesting]: The reasons for this are deeply interesting and have to do with the nitty gritty of how
Java is actually implemented. It might also change in the future.
109 changes: 109 additions & 0 deletions src/boxed_primitives/challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,112 @@ void main() {

## Challenge 2.

Write a method which takes in a `Integer[]` representing
a series of distances and prints out every distance
followed by ` kilometers`.

So if the array has `1`, `2`, and `3` you should output

```
1 kilometers
2 kilometers
3 kilometers
```

If this method is given `null`, it should act as if it
was given an empty array.


```java,editable
void printDistances(Integer[] distances) {
}
void main() {
printNames(new String[] {
45,
99,
23
});
}
```

## Challenge 3.

Write a method called `onlyPositive` which takes in an `int` and returns
the same value out if the number is greater than zero.

If the number is less than or equal to zero, return `null`.

```java,editable
// Write onlyPositive here
void main() {
// 45
System.out.println(
onlyPositive(45)
);
// 46
System.out.println(
onlyPositive(45) + 1
);
// null
System.out.println(
onlyPositive(0)
);
// null
System.out.println(
onlyPositive(-1)
);
}
```

## Challenge 4.

Will the following code work? Why or why not?

```java
void main() {
int ducks = 5;
Integer sparrows = 3;

int birds = ducks + sparrows;

System.out.println(birds);
}
```

## Challenge 4.

Will the following code work? Why or why not?

```java
void main() {
char[] face = new char[] { ':', ')' };
Character[] smile = face;

System.out.println(smile);
}
```

## Challenge 5.

Will the following code work? Why or why not?

```java
void main() {
char[] face = new char[] { ':', ')' };

Character[] smile = new Character[face.length];
for (int i = 0; i < face.length; i++) {
smile[i] = face[i];
}

System.out.println(smile);
}
```

## Challenge 6.
16 changes: 15 additions & 1 deletion src/boxed_primitives/character.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ Character c = null;
System.out.println(c);
c = '%';
System.out.println(c);
~}
~}
```

Unlike a `char[]`, a `Character[]` will not be have its contents
shown when printed.

```java
~void main() {
char[] c1 = new char[] { 'a', 'b', 'c' };
System.out.println(c1);

Character[] c2 = new Character[] { 'a', 'b', 'c' };
System.out.println(c2);
~}
```
13 changes: 12 additions & 1 deletion src/boxed_primitives/double.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ Double d = null;
System.out.println(d);
d = 3.14;
System.out.println(d);
~}
~}
```

If you try to do any math on a `Double` which holds `null` you will
get a `NullPointerException`.

```java,panics
~void main() {
Double d = null;
System.out.println(d + 1);
~}
```
10 changes: 10 additions & 0 deletions src/boxed_primitives/integer.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ System.out.println(i);
i = 5;
System.out.println(i);
~}
```

If you try to do any math on an `Integer` which holds `null` you will
get a `NullPointerException`.

```java,panics
~void main() {
Integer i = null;
System.out.println(i * 5);
~}
```

0 comments on commit a577e3f

Please sign in to comment.