COMPSCI4039
Programming
Revision – II
Copyright By PowCoder代写 加微信 powcoder
• Often, we need some way of ‘storing’ objects or primitives temporarily as we do some work.
• We can’t always work with things atomically.
• Arrays are a data structure.
• We use arrays in order to group things.
• Their sole purpose is to store primitive or objects.
• Arrays are often used to store elements that are of the same class/type.
• We can have arrays of ints, doubles, Strings or objects we define.
• What we cannot do is mix and match different types within arrays.
• Arrays are objects, like objects we call a constructor when we make them.
• But the syntax for creating arrays is unique to other objects.
• The general representation of an array is shown to
the right.
• T represents the type of an array, n represents the dimension of the array.
• Specifically, how many ‘boxes’ do we create in the array?
T[] array = new T[n];
Creating Arrays
• Arrays are a fixed length, once they are created, we cannot extend them.
• There are 2 ways we can create them: • Explicitly
• Methodically
• Explicitly – we pass in the data when we create it.
• Methodically – we pass in data to the relevant offsets afterwards.
Creating Arrays
Anatomy of an Array
• We access what is stored within an array via an offset.
• An offset is a reference, pointing to one logical container of the array data structure.
• The offsets are used to access the data in an array.
• The first element within an array is always stored at offset 0.
Anatomy of an Array
• If we had an array created with the following code:
int[] array = {1,2,3,4,5};
• We can get the 3rd element with:
int a = array[2];
• The code array[2] will point to the int value 7, as such we can treat array[2] like it were a primitive.
Anatomy of an Array
• If we had an array created with the following code:
Int[] array = {1,2,3,4,5};
• We can set the 3rd element with:
array[2] = 10;
• The code array[2] will point to the int value 7, as such we can set the value of array[2] like it were a primitive.
Look at this horrible code! (why we need loops)
• The problem with this code here is that it is not an efficient way to code.
• What happens when we want to do a lot of repeated operations?
• Work with huge arrays?
• What do we do?
• The answer is loops.
• Loops are one of the most important programmatic concepts to learn.
• They allow us to write and perform functions thousands, millions of
times over without having to code all of it out!
• Modern programming would be much, much more difficult and tedious without the use of loops.
• Loops are known as a control structure (or branching logic)
• Control structure is a way for us to control how our program executes lines of code.
How a for loop works
• Let’s dissect a very small program:
• We have a main method that specifies a for loop. • The for loop will print out the numbers 1 – 10
• Let’s see how it works.
How a for loop works
Loops require 3 key pieces of information to function:
1. Statement on what logic to execute before we start executing the loop logic.
Do note that this code is only ever executed once!
2. Statement on what condition should be met in order to execute the loop logic.
3. Statement on what logic to execute after the loop logic has been executed.
How a for loop works
How a for loop works
Loops + Arrays
• We use loops in order to perform operations on arrays.
• It allows us to perform a lot of operations with relatively little code.
• Let’s dissect this now with a small example:
• We create testArray with 3 elements in it.
• We want to print the values, incremented by 1
• When accessing contents in an array with a loop, we are creating an external counter variable that will be used to access each offset within the array.
• We finished the operation, the print statement, therefore we increment i by one.
• We can see thatiis still less than the length of the array, so we enter the code block again
• We finished the operation, the print statement, therefore we incremenet i by one.
• The condition is still true, so we enter the code block once again.
That is how we work with arrays and loops
When I is incrememted to the value of 3, we see that the condition is now false, therefore we leave the loop.
This prevents index out of bound errors (as there is no indice 3)
Multi-Dimensional Arrays
• We utilise nested for loops in order to work with multi-dimensional arrays.
• When we write code as follows: String[] anotherArray = new
String[3];
• We are creating a 1-dimensionsal array, because we are only passing in a single dimensional value (3).
• When we create 2d or say, 3d arrays, we pass in multiple dimension values in the following code syntax
String[][] twoDimArray = new String[3][2]; String[][][] threeDimArray = new String[3][2][4];
Working with multi-dimensional arrays
Let’s dissect the following example:
threedArray =
int[2][3][2];
When we run this code, we get the following data structure:
int[][][] threedArray = new int[2][3][2];
The first [2] dimensino specifies the first 2 offsets on the ‘top level’
int[][][] threedArray = new int[2][3][2];
The next [3] dimensino specifies that each offset in the first dimension contains an array of 3 dimensions.
This is noted in the second level.
int[][][] threedArray = new int[2][3][2];
The final [2] dimension value specifies that each offset in the second dimension contains an array of 2 dimensions.
This is noted in the bottom level.
threedArray[0][2][1] = 42;
The command above will be used to assign the value 42 to one of the offsets in the 3rd dimension.
We can see the ‘path’ that is taken to access it.
Nested for loops
• We use nested loops to iterate over arrays with multiple dimensions.
• The idea is to have mulitple individual values keep track of the offsets
we are looking at in the different dimensions.
• I will point to the indices of the first array dimension
• J will point to the indicies of the second array dimension
• K will point to the indices of the 3rd array dimension
int[][][] threedArray = new int[2][3][2];
for (int i = 0; i < threedArray.length; i++) {
for (int j = 0; j < threedArray[i].length; j++) {
for (int k = 0; k < threedArray[i][j].length; k++) {
System.out.println(threedArray[i][j][k]); }
Let's see this visualised
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com