From de823619aa6ef859e916e0673fc6cb76556ebf48 Mon Sep 17 00:00:00 2001 From: "Damith C. Rajapakse" Date: Tue, 7 Aug 2018 15:44:28 +0800 Subject: [PATCH] cppToJava/arrays, operators: tweak text --- cppToJava/dataTypes/arrays/text.md | 55 +++++++++++++++++++++------ cppToJava/dataTypes/operators/text.md | 46 +++++++++++----------- 2 files changed, 66 insertions(+), 35 deletions(-) diff --git a/cppToJava/dataTypes/arrays/text.md b/cppToJava/dataTypes/arrays/text.md index 2b5251f093..f9b050f976 100644 --- a/cppToJava/dataTypes/arrays/text.md +++ b/cppToJava/dataTypes/arrays/text.md @@ -13,35 +13,68 @@ Arrays are indicated using square brackets (`[]`). To create the array itself, you have to use the `new` operator. Here are some example array declarations: ```java int[] counts; +counts = new int[4]; // create an int array of size 4 + +int size = 5; double[] values; -counts = new int[4]; -values = new double[size]; -int[] scores = new int[4]; -double[] prices = new double[size]; +values = new double[size]; //use a variable for the size + +double[] prices = new double[size]; // declare and create at the same time +``` +
+Alternatively, you can use the shortcut syntax to create and initialize an array: + +```java +int[] values = {1, 2, 3, 4, 5, 6}; + +int[] anArray = { + 100, 200, 300, + 400, 500, 600, + 700, 800, 900, 1000 +}; ``` +{{ oracle }} +
-{{ similar }} The `[]` operator selects elements from an array. Array elements indices start from 0 %%i.e., the index of the first element is 0, not 1%%. +{{ similar }} The `[]` operator selects elements from an array. Array elements indices start from 0. ```java int[] counts = new int[4]; System.out.println("The first element is " + counts[0]); -counts[0] = 7; +counts[0] = 7; // set the element at index 0 to be 7 counts[1] = counts[0] * 2; -counts[2]++; +counts[2]++; // increment value at index 2 ``` -{{ different }} A Java array is aware of its size. +{{ different }} A Java array is aware of its size. A Java array prevents a programmer from indexing the array out of bounds. If the index is negative or not present in the array, the result is an error named `ArrayIndexOutOfBoundsException`. ```java int[] scores = new int[4]; -System.out.println(scores.length) +System.out.println(scores.length) // prints 4 +scores[5] = 0; // causes an exception ``` {{ icon_output }} ``` 4 +Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 + at Main.main(Main.java:6) ``` -{{ different }} A Java array prevents a programmer from indexing the array out of bounds. If the index is negative or not present in the array, the result is an error named `ArrayIndexOutOfBoundsException`. + +{{ similar }} It is also possible to create arrays of more than one dimension: +
+ +```java +String[][] names = { + {"Mr. ", "Mrs. ", "Ms. "}, + {"Smith", "Jones"} +}; + +System.out.println(names[0][0] + names[1][0]); // Mr. Smith +System.out.println(names[0][2] + names[1][1]); // Ms. Jones +``` +{{ oracle }} +
##### Passing arguments to a program @@ -61,8 +94,6 @@ You can run this program (after compiling it first) from the command line by typ `abc` -==//todo mention array initializing using literals== -
diff --git a/cppToJava/dataTypes/operators/text.md b/cppToJava/dataTypes/operators/text.md index c9e14934ce..0b77703eb9 100644 --- a/cppToJava/dataTypes/operators/text.md +++ b/cppToJava/dataTypes/operators/text.md @@ -14,11 +14,11 @@ Java has the following **arithmetic operators**: Operator | Description | Examples ---------|-------------|--------- -`+` | Additive operator | -`-` | Subtraction operator | -`*` | Multiplication operator | -`/` | Division operator | -`%` | Remainder operator | +`+` | Additive operator | `2 + 3` %%{{ icon_output_right }}%% `5` +`-` | Subtraction operator | `4 - 1` %%{{ icon_output_right }}%% `3` +`*` | Multiplication operator | `2 * 3` %%{{ icon_output_right }}%% `6` +`/` | Division operator | ==`5 / 2` %%{{ icon_output_right }}%% `2`== but ==`5.0 / 2` %%{{ icon_output_right }}%% `2.5`== +`%` | Remainder operator | `5 % 2` %%{{ icon_output_right }}%% `1` The following program uses some operators as part of an expression `hour * 60 + minute`: @@ -42,33 +42,33 @@ Number of minutes since midnight: 719 Operator | Description {{ oracle }} | example ---------|-------------|-------- -`+` | Unary plus operator; indicates positive value (numbers are positive without this, however) | -`-` | Unary minus operator; negates an expression | -`++` | Increment operator; increments a value by 1 | i++ -`--` | Decrement operator; decrements a value by 1 | i-- -`!` | Logical complement operator; inverts the value of a boolean | +`+` | Unary plus operator; indicates positive value
(numbers are positive without this, however) | `x = 5; y = +x` %%{{ icon_output_right }}%% `y` is `5` +`-` | Unary minus operator; negates an expression | `x = 5; y = -x` %%{{ icon_output_right }}%% `y` is `-5` +`++` | Increment operator; increments a value by 1 | `i = 5; i++` %%{{ icon_output_right }}%% `i` is `6` +`--` | Decrement operator; decrements a value by 1 | `i = 5; i--` %%{{ icon_output_right }}%% `i` is `4` +`!` | Logical complement operator; inverts the value of a boolean | `foo = true; bar = !foo` %%{{ icon_output_right }}%% `bar` is `false` **Relational operators** are used to check conditions like whether two values are equal, or whether one is greater than the other. The following expressions show how they are used: -Operator | Description | example ----------|-------------|-------- -`x == y` | x is equal to y | -`x != y` | x is not equal to y | -`x > y` | x is greater than y | -`x < y` | x is less than y | -`x >= y` | x is greater than or equal to y | -`x <= y` | x is less than or equal to y | +Operator | Description | example %%{{ icon_output_right }}%% `true`| example %%{{ icon_output_right }}%% `false` +---------|-------------|---------------------------------------|---------------------------------------- +`x == y` | `x` is equal to `y` | `5 == 5` | `5 == 6` +`x != y` | `x` is not equal to `y` |`5 != 6` | `5 != 5` +`x > y` | `x` is greater than `y` | `7 > 6` | `5 > 6` +`x < y` | `x` is less than `y` | `5 < 6` | `7 < 6` +`x >= y` | `x` is greater than or equal to `y` | `5 >= 5` | `4 >= 5` +`x <= y` | `x` is less than or equal to `y` | `4 <= 5` | `6 <= 5` The result of a relational operator is a boolean value. Java has three **conditional operators** that are used to operate on boolean values. -Operator | Description | example ----------|-------------|-------- -`&&` | | -`||` | | -`!` | | +Operator | Description | example %%{{ icon_output_right }}%% `true`| example %%{{ icon_output_right }}%% `false` +---------|-------------|-------------------------------------------|-------------------------------------------- +`&&` | and | `true && true` %%{{ icon_output_right }}%% `true` | `true && false` %%{{ icon_output_right }}%% `false` +`||` | or | `true || false` %%{{ icon_output_right }}%% `true` | `false || false` %%{{ icon_output_right }}%% `false` +`!` | not | `not false` | `not true`