160 likes | 170 Vues
Learn the definition, declaration, instantiation, and indexing of arrays. Understand why arrays are useful in storing and accessing data.
E N D
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] • [3.14159, 58008, 0, -123456789, 1000000000000] • [Dr. Pepper, Coke, Sprite, Pepsi] • [Collie, Dalmatian, Bulldog, Beagle, Rottweiler]
Declaration • An array is declared as follows: variable_type[] variable_name; • Examples: int[] costumerIDs; double[] changeJars; String[] NFLteamNames;
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];
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
Instantiating Examples int[] costumerIDs; costumerIDs = new int[1000]; double[] changeJars; changeJars = new double[6]; String[] NFLteamNames; NFLteamNames = new String[32];
Creating an Array at Declaration • 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};
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 we would put n-1 in the brackets.
Indexing Examples 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.
Indexing Activity rainbow = [red, orange, yellow, green, blue] rainbow[ 0 ] == ? rainbow[ 3 ] == ? rainbow[ ? ] == blue rainbow[ ? ] == green rainbow[ ? ] == orange
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”);
Daisy ArrayExample • 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();
Iteration • 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(inti = 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
Using .length in iteration • 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(inti = 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
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