CS代写 COMPSCI4039 Programming

COMPSCI4039 Programming

Why do we need arrays?
• Think of the first 5 prime numbers…

Copyright By PowCoder代写 加微信 powcoder

• Pretty easy for you to imagine a collection of numbers?
• Not easy for computers.
• We need to tell them specifically how we want to store a list of numbers.
• Or a list of anything really…
• Arrays are an example of a data structure that helps us do this. • You will learn about data structures in the ADS course.

What are they?
• They are containers.
• They allow us to store a list, or a collection of things.
• They are also objects.
• An array is an object, whose purpose is to store other objects, or primitives.
• They can store primitives:
• Integers, doubles, floats etc.
• They can also store Objects:
• Those from custom classes we define.
• Or from Java built in classes, like Scanner objects.

Visualizing Arrays
• Imagine an array as a row of boxes.
• When you create an array object, you are creating a row of boxes. • Each box contains something in it.
• Arrays can have only a single box… • …or they can have multiple boxes…

Rules of Arrays
• When creating an array, we need to do 2 things: • Specify what thing (or type), the array will store.
• Specify how large the array will be.
• You can think of it like this:
• Will the boxes store Cat objects? Dog objects? Integers? Doubles? Strings? • How many boxes will we have to store things?
• Each array ‘box’ or offset, can only store a single primitive, or object in it.
• This example here is an array of size 5, which stores integers.

Rules of Arrays
• When creating an array, we need to do 2 things: • Specify what thing (or type), the array will store.
• Specify how large the array will be.
• You can think of it like this:
• Will the boxes store Cat objects? Dog objects? Integers? Doubles? Strings? • How many boxes will we have to store things?
• Each array ‘box’ or offset, can only store a single primitive, or object in it.
• It will only store integer values, it cannot store a String for example.

Creating Arrays
• The formal syntax for creating arrays is as follows:
int[] myIntArray = new int[5];

Creating Arrays
• The formal syntax for creating arrays is as follows:
int[] myIntArray = new int[5];
Type appears at end, with the size of the array in square brackets. This array will have five elements
‘new’ tells us that Arrays are objects.
Array name
The use of the constructor for arrays is somewhat unique.
We don’t use ‘( )’ when creating them.
type now has [] added to denote an array.

Populating Arrays
• When we create arrays, they are initially empty.
• We need to add data to them.
• If we wanted to create an array of the first 5 prime numbers, we do:
• Which gives us the following:
int[] myIntArray = new int[5];
myIntArray[0] = 2;
myIntArray[1] = 3;
myIntArray[2] = 5;
myIntArray[3] = 7;
myIntArray[4] = 11;

Accessing Arrays
• When accessing arrays, we must refer to the ‘box’ or the offset that contains some value.
• We do this with [i] to indicate which specific object to access.
• Each array offset starts with 0.
• So if we want the first object, then i must be set to 0. • If we want the fifth prime number, i must be 4.
int x = myIntArray[0];
• This allows us to retrieve data from an array to do something with it!
• We could print it for example.

Accessing Arrays
• When accessing arrays, we must refer to the ‘box’ or the offset that contains some value.
• We do this with [i] to indicate which specific object to access.
• Each array offset starts with 0.
• So if we want the first object, then i must be set to 0. • If we want the fifth prime number, i must be 4.
int x = myIntArray[3];
• This allows us to retrieve data from an array to do something with it!

Final Note Here
• When creating arrays, we can do one of 2 things: • Either specify the array with new and then populate it. • Or specify the array with explicit values when created.
int[] myIntArray = new int[5];
myIntArray[0] = 2;
myIntArray[1] = 3;
myIntArray[2] = 5;
myIntArray[3] = 7;
myIntArray[4] = 11;
int[] myIntArray = {2,3,5,7,11};
• When creating values explicitly, you can create objects, instead of assigning primitives. Let’

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com