🍨
Array 📍
- Array is a sequence of values.
- In Java, all the values in an array must have the same type.
- The length of an array is established when the array is created. After creation, the length is fixed.
- All the values in an array are called elements.
Arrays Creation 📍
Without Initialisation 📍
The following code declares 4 arrays only, but not create them yet.
int[] a;
doulbe[] b;
char[] c;
boolean[] d;
// etc...
In order to create an array, we need to use the new
operator. The new
operator allocates memory for the array, and automatically initializes all its elements to zero.
a = new int[10];
b = new double[100];
c = new char[1000];
d = new boolean[10000];
// etc..
Fo sho, you can also declare and create an array in one line.
int[] a = new int[10];
double[] b = new double[100];
char[] c = new char[1000];
boolean[] d = new boolean[10000];
// etc...
For the size of an array, you can use an integer variable or an integer expression, and make sure the value is non-negative. Otherwise, you will get a NegativeArraySizeException
. In general, array.length
0 Size of an Array 📍
int[] emptyArray = new int[0];
Java allocates memory for the array because of the new
operator, but it doesn't allocate memory for the elements. So it's not a good idea to access the elements of an empty array. If you do, you will get a ArrayIndexOutOfBoundsException
because the index range for this array will be from
Initialisation 📍
You can also initialise an array with a comma-separated sequence of elements enclosed in braces.
int[] a = {1, 99};
double[] b = {1.0, 99.4, 32.3};
char[] c = {'h', 'e', 'l', 'l', 'o'};
boolean[] d = {true, false, true, false};
// etc...
Array Access 📍
Suppose we have an array as follows.
int[] a = mew int[5];
Index 📍
The elements in an array are numbered from
Assignment 📍
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
Selection 📍
System.out.println(a[0]); // 1
Elements Update 📍
a[0] = 99; // 1 -> 99
a[1] = a[0] + 1; // 2 -> 100
a[2]++; // 3 -> 4
a[3] += 2; // 4 -> 6
a[4] -= 3; // 5 -> 2
Array Length 📍
System.out.println(a.length); // 5
Array Iteration 📍
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
Array Display 📍
Sometimes, we may want to display an array, but you would get something unexpected. Directly print an array will get the following result, which is the memory address of the array.
[
means array, I
means integer, @
means at, and 15db9742
is the memory address of the array.
System.out.println(a); // [I@15db9742
Of course you can print the elements using loops as Array Iteration above.
Additionally, you can use the Arrays
class to display an array. In Java library, java.util.Arrays
provides a method called toString
to display an array.
System.out.println(Arrays.toString(a)); // [99, 100, 4, 6, 2]
Array Copy 📍
Array variables contain references to arrays. When you assign an array variable to another, you are copying the reference. So the two variables refer to the same array. If you modify one, the other changes as well.
Suppose we have another array b
that copies array a
as follows.
int[] b = a; // Use the same memory address as `a`
// Which means if you make changes on `b`, `a` will change as well.
If you want to copy an array not just the reference, you can do as follows.
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
You can also use java.util.Arrays
to copy an array. It provides a method called copyOf
.
int[] b = Arrays.copyOf(a, a.length);
Array Enhanced Iteration 📍
// Create a variable `val` to store the value of each element in the array.
// Data type should be also the same as the array.
for (int val : a) {
System.out.println(a);
}
Random Numbers 📍
(Non) Determinism 📍
Determinism 📍
💡 Determinism: An algorithm or automation follows a single, well-defined sequence of steps for a given input. For those algorithms, they are predictable and produce the same input following the same path of execution.
Nondeterminism 📍
💡 Nondeterminism: An algorithm or automation has multiple possible paths it can follow for a given input and starting state.
Making a program nondeterministic turns out to be hard, that's because it's impossible for a computer to generate truly random numbers (Similar to human's brain, we can come up with a random number out of nowhere. But for computer, a random number can only be generated by a specific algorithm). But there are algorithms that generate unpredictable sequences called pseudorandom numbers. For most applications, they are as good as random.
Click HERE for a video example of Deep Learning Cars
Library 📍
import java.util.Random;
Method 📍
Random random = new Random();
int a = 0;
double b = 1;
a = random.nextInt(100);
// Common: nextDouble(), nextFloat(), nextLong()
nextInt
takes an integer argument, 100
, and returns a random integer between 0
and 100-1
(inclusive). For the other types, they follow the same rule.
Q & A 📍
Q1: Waiting~
A1: Waiting for the Q1's Waiting~