Today in class, we learned about parallel arrays. These are essentially two arrays that correspond to each other. For example, we can have an String[] employees array that corresponds to an int[] workHours array. These aren't related in a technical sense, so changing one array means that you must change the other one. We call them parallel because the first index of one corresponds to the first index of the other(s), the second one corresponds to the second, and so on.
The issue with parallel arrays is that they aren't actually related, so it can be very easy to lose track of each array and the correspondence can be broken.
We also learned about 3D dimensional arrays. We already know how to create 2D arrays, but can we go further? The answer is yes. To initialize a 3D array, we use the following syntax:
dataType[][][] array = new dataType[x][y][z]; //where x, y, and z are different lengths of each "dimension" of an array (i.e. length, width, height)
In Java, we can actually initialize arrays of any number of dimensions. However, it's hard to keep track of arrays that are more than three dimensions because we live in a three-dimensional world.
Remember that 2D and 3D arrays are just arrays of arrays. This will help you use them more effectively and they will over time become less confusing.
And since they are just arrays of arrays, we can make each array in the multi-dimensional array a different length. These are called ragged arrays. The simplest way to initialize these are to create the first array but with no columns:
dataType[][] array = new dataType[x][]; //where x is the number of subarrays you want
And then to initialize each of the arrays in the first array:
array[0] = new dataType[1];
array[1] = new dataType[2];
array[2] = new dataType[3];
...
array[x-1] = new dataType[2];
An example of a use of a ragged array is a school, where each one has different classes of different sizes.
The final concept we learned in class was copying arrays.
When copying variables, we can just do the following:
int x = 35;
int y = x;
and the integer y will be 35.
When we try this with arrays, no error will pop up, but we will notice something strange when we change values in the original array:
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = array1;
array1[0] = 5;
System.out.println(array2[0]); //this outputs 5 - why?
So what happened? When we created the second array, we actually assigned the first array to the second - so when Java sees this, they think we are referring to the same array. In other words, array2 is just an alias for array1. In the memory, they both are associated with the same memory blocks.
We can avoid this by using a for loop to copy arrays, or by using the Arrays.copyOf() method or the System.arraycopy utility.
Solution:
class Main { public static void main(String[] args) { int[][] array = new int[4][2]; for(int i = 0; i < array.length; i++) { for(int j = 0; j < array[i].length; j++) { array[i][j] = 3; System.out.println(array[i][j]); } } } }
Homework is here: https://docs.google.com/forms/d/e/1FAIpQLSf0NEruyfOunijj1TVBls6HEKph7p6N1nw8vhsG8KcrZpuKcQ/viewform?usp=sf_link