Spaces:
Running
Running
File size: 3,361 Bytes
ec3e65d |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
Arrays are static data structures: their SIZE is defined AT INITIALISATION and cannot be changed afterwards.
It is therefore not possible to add or remove primitives from an array.
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.
// datatype of elements
// empty square brackets []
// variable name
// 'new' operator - define the array object
// datatype of elements
// num_elements
public class Example {
public static void main(String[] args){
// Array in which 10 integers
// can be saved
int[] numarray = new int[10];
// Array for strings
String[] names = new String[20];
// Array for floating-point numbers
double[] results = new double[5];
}
}
At initialisation, fill array with default values
- integer 0
- boolean false
- null
VS
// initialise Array with values
// Array with five integers
int[] numarray = {1, 3, 2, 5, -1};
// Array with strings
String[] names = {"Jack", "Pete", "Jane", "Lisa"};
// Array with floating-point numbers
double[] results = {1.0, 2.5, 0.75};
======================================================
import java.util.Arrays;
public class Example {
public static void main(String[] args){
int[] numbers = new int[5];
numbers[0] = 10; // variableName[idx]
numbers[1] = 40;
numbers[4] = -155;
// When printing an array the method
// Arrays.toString is useful.
System.out.println(Arrays.toString(numbers)); // [10, 40, 0, 0, -155]
double[] heights = {175.5, 150.75, 201.05};
System.out.println(heights[0]);
heights[1] = heights[1] + 10; // variableName[idx]
System.out.println(heights[1]); // variableName[idx]
}
}
Program outputs:
[10, 40, 0, 0, -155]
175.5
160.75
======================================================
length
The length of the array (i.e. the number of sub-arrays) is determined by the length attribute.
The extended for clause can also be used to iterate through an array in the same way as iterating through a list.
import java.util.Arrays;
public class Example {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7};
System.out.println("Array length: " + numbers.length);
for (int element : numbers) {
System.out.println(element);
}
for (int i=0; i<numbers.length; i++) {
numbers[i]++;
}
System.out.println(Arrays.toString(numbers));
}
}
Program outputs:
Array length: 7
1
2
3
4
5
6
7
[2, 3, 4, 5, 6, 7, 8]
======================================================
Note that unlike a string, ARRAY LENGTH is an attribute
ARRAY length is not a method - it is not followed by parentheses.
The example below further illustrates the differences between length detection for a string, an array and a "list":
int[] array = {1,2,3,4,5,6,7};
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
String str = "Hey";
System.out.println("Array length: " + array.length);
System.out.println("List length: " + list.size());
System.out.println("String length: " + str.length());
Program outputs:
Array length: 7
List length: 2
String length: 3
|