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
+ +```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== -