Transcript Arrays

[Array, Array, Array, Array]
 Definition - A group of homogeneous elements of a
specific data type*
 Examples:
 [a,e,t,w,e,e,y,u,f,v,x,g,h,q,w,r,f,d,a,h,o,a,m,s,z,d]
 [3.14159, 58008, 0, -123456789, 1000000000000]
 [Dr. Pepper, Coke, Sprite, Pepsi]
 [Collie, Dalmatian, Bulldog, Beagle, Rottweiler]
 An array is declared as follows:
variable_type[] variable_name;
 Examples:
int[]
costumerIDs;
double[] changeJars;
String[] NFLteamNames;
int
int
 An array though is an object so declaring it
only gives us a reference to a sequence of
primitives or object references that are not
created when the array is declared.
 We must use ‘new’ to create the primitives
or object references using the following
syntax:
variable_name = new variable_type[size];
int
variable_name = new variable_type[size];
 In this declaration, size is the number of objects or
primitives being created.
 size and must be an integer.
 Size of an array is set when the array is
created(instantiated).
 Once set, the size of an array can not be changed!
 There are other data structures though that will let us
change the size
int[] costumerIDs;
costumerIDs = new int[1000];
double[] changeJars;
changeJars = new double[6];
String[] NFLteamNames;
NFLteamNames = new String[32];
 We can create an array of primitives at the time that we
declare the array in the following way:
double [] numbers = {5.2, -8.32, 0.16};
boolean [] values = {true, false, false};
char [] letters = {‘a’, ‘b’, ‘x’};
 This type of assignment can only be done when the array is
declared. The following is illegal:
double [] numbers;
numbers = {5.2, -8.32, 0.16};
 To get an element in an array then write the variable
name followed by brackets with an integer in between
the brackets:
array_varaible[integer]
 This can be tricky as indexing starts at 0. So in order
to reference the nth value in the array then we would
put n-1 in the brackets.
We have the following array:
double [] numbers = {5.2, -8.32, 0.16};
Then
numbers[0] == 5.2
numbers[1] == -8.32
numbers[2] == 0.16
Any integer greater than 2 or less than 0 is out of bounds
for this array.
rainbow = [red, orange, yellow, green, blue]
rainbow[ 0 ] == ? red
rainbow[ 3 ] == ? green
rainbow[ 4? ] == blue
rainbow[ 3? ] == green
rainbow[ 1? ] == orange
 Just like when we declare an objects variables, we must
instantiate each object in an array.
 Example:




Toy[]
ToyBox;
ToyBox = new Toy[2];
ToyBox[0] = new Toy(“Woody”);
ToyBox[1] = new Toy(“Buzz”);
 Step 1 – Declare the array
 Daisy[] DaisySet;
 Step 2 – Instantiating Array
 DaisySet = new Daisy[3];
 Step 3 – Instantiate Objects in the Array
 DaisySet[0] = new Daisy();
 DaisySet[1] = new Daisy();
 DaisySet[2] = new Daisy();
 We can use loops to cycle through all the elements of
an array. The following loop will print all the elements
of the array nums assuming there are ten elements
for(int i = 0; i < 10; i++){
System.out.println(nums[i]);
}
 Notice that the loop variable i is being used as our
index so that as we go through the loop, we reference
the next element in the array.
 Also note that i starts at 0 and goes up to but does not
go to 10 which would be out of bounds
 If the length of the array is not known, we can use
.length to obtain the number of elements and use it in
our code like so:
for(int i = 0; i < nums.length; i++){
System.out.println(nums[i]);
}
 This is especially useful if our loop is to work with
arrays of varying lengths
 If we can’t change the size of arrays and there is such a
primitive interface, why use arrays?
 Arrays more closely represent the physical reality of
how data is stored on a computer and in general how
anything is stored.
 Example: Books in a Library