Spaces:
Running
Running
String[] names = new String[16]; arr.length; list.size(); str.length();
Browse files
Week 3: Objects, files and exceptions/9a. Data structure - ARRAY
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Arrays are static data structures: their SIZE is defined AT INITIALISATION and cannot be changed afterwards.
|
| 2 |
+
It is therefore not possible to add or remove primitives from an array.
|
| 3 |
+
In general, a list is a more useful structure, but some Java methods return an array- so it's a good idea to know how to use them.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
// datatype of elements
|
| 8 |
+
// empty square brackets []
|
| 9 |
+
// variable name
|
| 10 |
+
// 'new' operator - define the array object
|
| 11 |
+
// datatype of elements
|
| 12 |
+
// num_elements
|
| 13 |
+
|
| 14 |
+
public class Example {
|
| 15 |
+
public static void main(String[] args){
|
| 16 |
+
// Array in which 10 integers
|
| 17 |
+
// can be saved
|
| 18 |
+
int[] numarray = new int[10];
|
| 19 |
+
|
| 20 |
+
// Array for strings
|
| 21 |
+
String[] names = new String[20];
|
| 22 |
+
|
| 23 |
+
// Array for floating-point numbers
|
| 24 |
+
double[] results = new double[5];
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
At initialisation, fill array with default values
|
| 30 |
+
- integer 0
|
| 31 |
+
- boolean false
|
| 32 |
+
- null
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
VS
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
// initialise Array with values
|
| 40 |
+
// Array with five integers
|
| 41 |
+
int[] numarray = {1, 3, 2, 5, -1};
|
| 42 |
+
|
| 43 |
+
// Array with strings
|
| 44 |
+
String[] names = {"Jack", "Pete", "Jane", "Lisa"};
|
| 45 |
+
|
| 46 |
+
// Array with floating-point numbers
|
| 47 |
+
double[] results = {1.0, 2.5, 0.75};
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
======================================================
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
import java.util.Arrays;
|
| 60 |
+
|
| 61 |
+
public class Example {
|
| 62 |
+
public static void main(String[] args){
|
| 63 |
+
int[] numbers = new int[5];
|
| 64 |
+
numbers[0] = 10; // variableName[idx]
|
| 65 |
+
numbers[1] = 40;
|
| 66 |
+
numbers[4] = -155;
|
| 67 |
+
|
| 68 |
+
// When printing an array the method
|
| 69 |
+
// Arrays.toString is useful.
|
| 70 |
+
System.out.println(Arrays.toString(numbers)); // [10, 40, 0, 0, -155]
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
double[] heights = {175.5, 150.75, 201.05};
|
| 74 |
+
System.out.println(heights[0]);
|
| 75 |
+
|
| 76 |
+
heights[1] = heights[1] + 10; // variableName[idx]
|
| 77 |
+
System.out.println(heights[1]); // variableName[idx]
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
Program outputs:
|
| 83 |
+
[10, 40, 0, 0, -155]
|
| 84 |
+
175.5
|
| 85 |
+
160.75
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
======================================================
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
length
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
The length of the array (i.e. the number of sub-arrays) is determined by the length attribute.
|
| 104 |
+
The extended for clause can also be used to iterate through an array in the same way as iterating through a list.
|
| 105 |
+
|
| 106 |
+
import java.util.Arrays;
|
| 107 |
+
|
| 108 |
+
public class Example {
|
| 109 |
+
public static void main(String[] args){
|
| 110 |
+
int[] numbers = {1,2,3,4,5,6,7};
|
| 111 |
+
System.out.println("Array length: " + numbers.length);
|
| 112 |
+
|
| 113 |
+
for (int element : numbers) {
|
| 114 |
+
System.out.println(element);
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
for (int i=0; i<numbers.length; i++) {
|
| 118 |
+
numbers[i]++;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
System.out.println(Arrays.toString(numbers));
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
Program outputs:
|
| 126 |
+
Array length: 7
|
| 127 |
+
1
|
| 128 |
+
2
|
| 129 |
+
3
|
| 130 |
+
4
|
| 131 |
+
5
|
| 132 |
+
6
|
| 133 |
+
7
|
| 134 |
+
[2, 3, 4, 5, 6, 7, 8]
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
======================================================
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
Note that unlike a string, ARRAY LENGTH is an attribute
|
| 151 |
+
ARRAY length is not a method - it is not followed by parentheses.
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
The example below further illustrates the differences between length detection for a string, an array and a "list":
|
| 156 |
+
|
| 157 |
+
int[] array = {1,2,3,4,5,6,7};
|
| 158 |
+
|
| 159 |
+
ArrayList<Integer> list = new ArrayList<>();
|
| 160 |
+
list.add(1);
|
| 161 |
+
list.add(2);
|
| 162 |
+
|
| 163 |
+
String str = "Hey";
|
| 164 |
+
|
| 165 |
+
System.out.println("Array length: " + array.length);
|
| 166 |
+
System.out.println("List length: " + list.size());
|
| 167 |
+
System.out.println("String length: " + str.length());
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
Program outputs:
|
| 171 |
+
Array length: 7
|
| 172 |
+
List length: 2
|
| 173 |
+
String length: 3
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|