Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays [Array, Array, Array, Array] Our Very First Data Structure! 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]

Similar presentations


Presentation on theme: "Arrays [Array, Array, Array, Array] Our Very First Data Structure! 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]"— Presentation transcript:

1

2 Arrays [Array, Array, Array, Array]

3 Our Very First Data Structure! 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]

4 Make Your Own Array!

5 Declaration An array is declared as follows: variable_type[]variable_name; Examples: int[]costumerIDs; double[]changeJars; String[]NFLteamNames;

6 Instantiating 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 int

7 Array Size 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

8 Instantiating Examples int[]costumerIDs; costumerIDs = new int[1000]; double[]changeJars; changeJars = new double[6]; String[]NFLteamNames; NFLteamNames = new String[32];

9 Indexing Arrays 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 would put n-1 in the brackets.

10 Indexing Examples We have the following array: 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.

11 Indexing Activity rainbow = [red, orange, yellow, green, blue] rainbow[ 0 ] == ? rainbow[ 3 ] == ? rainbow[ ? ] == blue rainbow[ ? ] == green rainbow[ ? ] == orange red green 4 3 1

12 Instantiating Arrays of Objects 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”);

13 Daisy Array 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();

14 intcounter; //a counter variable for a loop //declare the array int[]multiples; //initialize the array multiples = new int[10]; //for loop that sets the values of the array to multiples of 5 for(counter = 1; counter <= 10; counter ++){ multiples[counter - 1] = 5 * counter; } Initializing and Indexing Activity

15 Indexing (cont.) for(counter = 7; counter > -3; counter --){ multiples[-counter+7] = 6 * (-counter+8); } for(counter = 0; counter <= 9; counter ++){ multiples[counter] = 3 * (counter+1); }

16 Why use arrays? 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


Download ppt "Arrays [Array, Array, Array, Array] Our Very First Data Structure! 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]"

Similar presentations


Ads by Google