-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.html
87 lines (79 loc) · 5.21 KB
/
arrays.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html lang="en">
<head>
<title>Arrays</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="index.js"></script>
<link rel="icon" type="image/x-icon" href="images/star.png">
</head>
<body onload="head(); dateTime(); foot();">
<header id="header" style="position:fixed;"></header>
<h1 class="title">Arrays</h1>
<div class="text">
<h3>What are Arrays?</h3>
<p>Arrays are a block of consecutive memory locations that hold values of the same data type, with the individual locations being called elements. Rather than treating each element as a separate variable, the whole array gets one name, which makes code more efficient if there are multiple similar variables (ex. student1, student2...). <br><br>The specific elements in an array are referred to by its index, which is an int value from 0 to length-1. Arrays have fixed size and thus their length cannot be changed. Note that the Java interpreter checks the values of indices at runtime and throws ArrayIndexOutOfBoundsException if an index is not possible to access.<img src="images/array.png" width="70%" height="auto"></p>
<h3><br>Parallel Arrays and Syntax</h3>
<p>Before use, arrays must be declared. The syntax used is <span class="textCode">anyType[] arrName;</span> Declaration only creates a reference set to null, and thus arrays must be initialized before use. Arrays are objects, and are thus declared with the new keyword. <br>
<br><br> Different ways of initialization:
<ul>
<li>No values: <span class="textCode">anyType[] arrName = new anyType[length];</span></li>
<li>Preset values: <span class="textCode">anyType[] arrName = {element, element, ...};</span></li>
</ul>
When arrays are initialized without values, all elements are set to default values of either 0 for numbers, false for booleans, or null for objects. The length of array is either given explicitly or comes from the length of the {...} initialization list. To access the length of an array, use arrName.length, which is a public field in an array object
<br><br>All array elements can be accessed with the syntax <span class="textCode">arr[k]</span>, indicating the k-th index. The first element is a[0] and the n-th element is a[n-1].</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
int[] arr1 = new int[5]; //array of size 5 with default values
double[] arr2 = {0.1, 0.2, 0.3}
System.out.print(arr2[1]); //prints 0.2
</pre>
</div>
<p><br><br>Passing arrays into methods: <span class="textCode">public void method(anyType[] arrName)</span>. The elements of the original array are not copies as with primitive data types, so they are accessible and mutable in the method’s code.
<br><br>Returning arrays from methods: <span class="textCode">public anyType[] method()</span></p>
<h3><br>2D Arrays and Syntax</h3>
<p>2D arrays are in the form of a rectangular board. They are inherently an array of arrays. Each row is stored in a separate block of consecutive memory locations. Elements are accessed using the row then column (ex. board[r][c]). Like parallel arrays, row and column indices start at 0. <br><img src="images/2darray.png" width="70%" height="auto">
<br><br>Declaration: <span class="textCode">anyType[][] arrName;</span>
<br>Initialization:
<ul>
<li>No values: <span class="textCode">arrName = new anyType[rows][columns];</span></li>
<li>Preset values: <span class="textCode">anyType[][] arrName = {{element, element}, {element, element}}</span></li>
</ul>
arrName.length is number of rows, and arrName[n].length is length of the n-th row.</p>
<h3><br>Traversing Arrays</h3>
<p>Parallel arrays: </p>
<ul>
<li>Using a for loop: <span class="textCode">for (int i = 0; i < arr.length; i++) //process arr[i]</span></li>
<li>Using a for each loop:</li>
<ul>
<li>Introduced in Java 5 and is convenient for traversing arrays</li>
<li>Syntax: <span class="textCode">for (int i : arr)</span> - “for int i in arr”</li>
<li>Downside is that elements cannot be changed</li>
</ul>
</ul>
<p>2D arrays:</p>
<ul>
<li>Using a nested for loop:</li>
<div class="code">
<pre>
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr[i].length; j++){
//process arr[i][j]
}
}
</pre>
</div>
</ul>
<h2><br>Common Mistakes</h2>
<ol>
<li>Forgetting to declare the length</li>
<li>Forgetting that array indices start with 0</li>
<li>Tring to access array indices out of bounds</li>
<li>Switching the row an column order with 2D arrays</li>
</ol>
</div>
<footer id="footer"></footer>
</body>
</html>