|
1 | | -# Arrays |
| 1 | +# Introduction |
2 | 2 |
|
3 | | -In Java, data structures that can hold zero or more elements are known as _collections_. An **array** is a collection that has a fixed size/length and whose elements must all be of the same type. Elements can be assigned to an array or retrieved from it using an index. Java arrays are zero-based, meaning that the first element's index is always zero: |
| 3 | +## Arrays |
| 4 | + |
| 5 | +In Java, data structures that can hold zero or more elements are known as _collections_. |
| 6 | +An **array** is a collection that has a fixed size and whose elements must all be of the same type. |
| 7 | +Elements can be assigned to an array or retrieved from it using an index. |
| 8 | +Java arrays use zero-based indexing: the first element's index is 0, the second element's index is 1, etc. |
| 9 | + |
| 10 | +Here is the standard syntax for initializing an array: |
| 11 | + |
| 12 | +```java |
| 13 | +type[] variableName = new type[size]; |
| 14 | +``` |
| 15 | + |
| 16 | +The `type` is the type of elements in the array which may be a primitive type (e.g. `int`) or a class (e.g. `String`). |
| 17 | + |
| 18 | +The `size` is the number of elements this array will hold (which cannot be changed later). |
| 19 | +After array creation, the elements are initialized to their default values (typically `0`, `false` or `null`). |
4 | 20 |
|
5 | 21 | ```java |
6 | 22 | // Declare array with explicit size (size is 2) |
7 | 23 | int[] twoInts = new int[2]; |
| 24 | +``` |
| 25 | + |
| 26 | +Arrays can also be defined using a shortcut notation that allows you to both create the array and set its value: |
8 | 27 |
|
| 28 | +```java |
| 29 | +// Two equivalent ways to declare and initialize an array (size is 3) |
| 30 | +int[] threeIntsV1 = new int[] { 4, 9, 7 }; |
| 31 | +int[] threeIntsV2 = { 4, 9, 7 }; |
| 32 | +``` |
| 33 | + |
| 34 | +As the compiler can now tell how many elements the array will have, the length can be omitted. |
| 35 | + |
| 36 | +Array elements may be assigned and accessed using a bracketed index notation: |
| 37 | + |
| 38 | +```java |
9 | 39 | // Assign second element by index |
10 | 40 | twoInts[1] = 8; |
11 | 41 |
|
12 | 42 | // Retrieve the second element by index and assign to the int element |
13 | | -int element = twoInts[1]; |
| 43 | +int secondElement = twoInts[1]; |
14 | 44 | ``` |
15 | 45 |
|
16 | | -Arrays can also be defined using a shortcut notation that allows you to both create the array and set its value. As the compiler can now tell how many elements the array will have, the length can be omitted: |
| 46 | +Accessing an index that is outside of the valid indexes for the array results in an `IndexOutOfBoundsException`. |
| 47 | + |
| 48 | +Arrays can be manipulated by either calling an array instance's methods or properties, or by using the static methods defined in the `Arrays` class (typically only used in generic code). |
| 49 | +The most commonly used property for arrays is its length which can be accessed like this: |
17 | 50 |
|
18 | 51 | ```java |
19 | | -// Two equivalent ways to declare and initialize an array (size is 3) |
20 | | -int[] threeIntsV1 = new int[] { 4, 9, 7 }; |
21 | | -int[] threeIntsV2 = { 4, 9, 7 }; |
| 52 | +int arrayLength = someArray.length; |
22 | 53 | ``` |
23 | 54 |
|
24 | | -Arrays can be manipulated by either calling an array instance's methods or properties, or by using the static methods defined in the `Arrays` class. |
| 55 | +Java also provides a helpful utility class [`java.util.Arrays`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html) that has lots of useful array-related methods (eg. `Arrays.equals`). |
| 56 | + |
| 57 | +Java also supports [multi-dimensional arrays](https://www.programiz.com/java-programming/multidimensional-array) like `int[][] arr = new int[3][4];` which can be very useful. |
25 | 58 |
|
26 | | -The fact that an array is also a _collection_ means that, besides accessing values by index, you can iterate over _all_ its values using a `foreach` loop: |
| 59 | +The fact that an array is also a _collection_ means that, besides accessing values by index, you can iterate over _all_ its values using a `for-each` loop: |
27 | 60 |
|
28 | 61 | ```java |
29 | 62 | char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; |
30 | 63 |
|
31 | | -for(char vowel:vowels) { |
| 64 | +for(char vowel: vowels) { |
32 | 65 | // Output the vowel |
33 | 66 | System.out.print(vowel); |
34 | 67 | } |
|
0 commit comments